|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Quick Questions on threadingI've been toying with threads the past couple days and I
was curious if anyone could answer a few basic questions. 1) I'd like to take a method call I make currently successfully, and simply throw it into a new thread to run until the method finishes, at which point in time the thread would terminate. What would be the best approach to do this. I dont ind doing some reading... I tried to be resourcefull ahead of time. Hmm I counted only one question ;-)
How to make the call on another thread depends on what that thread is going to do. If its a long running activity or you want full control over te thread then create a new instance of the Thread class and run the method on that (there are issues of how you supply parameters but we'll come to that if you neeed that facility). if you simply want to run the method on another thread and its not particularly long running then use the CLR threadpool by creating a delegate that wraps the method and call BeginInvoke on the delegate instance. Youo will have to call EndInvoke on the instance too but there are a few ways to do that. I write an article on async delegate invocation a while back: http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html Regards Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk I've been toying with threads the past couple days and I was curious if anyone could answer a few basic questions. 1) I'd like to take a method call I make currently successfully, and simply throw it into a new thread to run until the method finishes, at which point in time the thread would terminate. What would be the best approach to do this. I dont ind doing some reading... I tried to be resourcefull ahead of time. Well the thread is going to fire off a method to populate
webcache. It normally takes about 1-2minutes, which I dont know if you'd consider is a long running task. I'd like to use the fire and forget method, then fire off "LoadData(var)" and have it fetcht he data into the cache. Heres what I do now: new Thread (new ThreadStart (BusAllPromoCF.LoadCacheItem)).Start(); from my Application_Start and that does succesffully spawn a thread and load data into the cache. When the CacheItemRemovedCallBack gets fired, I need to reload data on a seperate thread. I want to make sure I am not spawing a buncha threads only to have them take up unnecessary resources. Thanks., >-----Original Message----- that thread is going to do. If its a long running activity >Hmm I counted only one question ;-) > > How to make the call on another thread depends on what or you want full control over te thread then create a new instance of the Thread class and run the method on that (there are issues of how you supply parameters but we'll come to that if you neeed that facility). if you simply want to run the method on another thread and its not particularly long running then use the CLR threadpool by creating a delegate that wraps the method and call BeginInvoke on the delegate instance. Youo will have to call EndInvoke on the instance too but there are a few ways to do that. I write an article on async delegate invocation a while back: > http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelega> tes.html Show quoteHide quote > > Regards > > Richard Blewett - DevelopMentor > http://www.dotnetconsult.co.uk/weblog > http://www.dotnetconsult.co.uk > > I've been toying with threads the past couple days and I > was curious if anyone could answer a few basic questions. > > 1) I'd like to take a method call I make currently > successfully, and simply throw it into a new thread to run > until the method finishes, at which point in time the > thread would terminate. > > What would be the best approach to do this. I dont ind > doing some reading... I tried to be resourcefull ahead of > time. > >. > "Weston Weems" <anonym***@discussions.microsoft.com> wrote in message The first thing you should do is to determine whether your existing method news:01bf01c4f2ab$98df0aa0$a401280a@phx.gbl... > I've been toying with threads the past couple days and I > was curious if anyone could answer a few basic questions. > > 1) I'd like to take a method call I make currently > successfully, and simply throw it into a new thread to run > until the method finishes, at which point in time the > thread would terminate. > > What would be the best approach to do this. I dont ind > doing some reading... I tried to be resourcefull ahead of > time. uses any instance data. In particular, if you can set the method to be static (in C#) or Shared (in VB), then it doesn't use any instance data, and you're ok. If it _does_ use instance data, then you may need to synchronize access to that data betweeen the main thread and your new thread. John Saunders <"John Saunders" <johnwsaundersiii at hotmail.com>> wrote: Just because a method is static doesn't mean it doesn't need to do any > The first thing you should do is to determine whether your existing > method uses any instance data. In particular, if you can set the > method to be static (in C#) or Shared (in VB), then it doesn't use > any instance data, and you're ok. > > If it _does_ use instance data, then you may need to synchronize > access to that data betweeen the main thread and your new thread. synchronization - it could easily still be using shared data. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message Yes, Jon is correct. Synchronization may be needed for accessing anything news:MPG.1c455c1f25ee144798bb98@msnews.microsoft.com... > <"John Saunders" <johnwsaundersiii at hotmail.com>> wrote: >> The first thing you should do is to determine whether your existing >> method uses any instance data. In particular, if you can set the >> method to be static (in C#) or Shared (in VB), then it doesn't use >> any instance data, and you're ok. >> >> If it _does_ use instance data, then you may need to synchronize >> access to that data betweeen the main thread and your new thread. > > Just because a method is static doesn't mean it doesn't need to do any > synchronization - it could easily still be using shared data. which can be reached from multiple threads. John Saunders
Other interesting topics
general question regarding when to abstract vs. interface
Proper place for custom CONFIG file Exporting registry key to a reg file? String conversion from an Object in VB.NET Enum conversion from an Object in VB.NET Error 1937 on VS installation Silent install of .Net Framework 1.1 Change of .NET framework website KISS Tutorials Unable to reuse remoting port |
|||||||||||||||||||||||