threads and singleton objects
If I have a singleton object that is created when a .NET web application is started and then I spawn a new thread from that application, can the child thread see and use the singleton object?
Laurence -
[211 byte] By [
Hunterlmc] at [2007-11-11 10:30:41]

# 1 Re: threads and singleton objects
I believe so. Basically you want to know if you create an object on one thread can another thread use it. I've done this without a problem. The only thing to consider is possible syncronization issues. If any possible issues exist they'll need to be taken care of some how.
# 2 Re: threads and singleton objects
Also remember that if the singleton is a form, then you need to use Invoke / BeginInvoke if you are gonna do any GUI-work from the other thread.
# 3 Re: threads and singleton objects
What I'm going to trying to do is store used command objects within a collection contained within the singleton object. The thread is there to monitor the singleton object and remove command objects that aren't used within a given amount of time (to prevent what could be a massive memory leak).
I figure that way, I'll save the time required to constantly recreate and configure command objects for those stored procedures that are most commonly used by the application as it runs.
Laurence -
# 4 Re: threads and singleton objects
Threading does not affect scope, so as long as the singleton object can be seen by the thread routine, you are OK.
If you will have multiple threads accessing the object, make sure you read up on syncronization and possible issues.
darren at 2007-11-11 20:50:35 >
