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?
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.
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.
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.
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.
By hemanthjava in forum Java
Last Post: 11-14-2006, 10:57 PM
By Gwyn Carwardine in forum .NET
Last Post: 04-29-2002, 10:01 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL