Difference between Thread.Start() and Delegate.BeginInvoke()
-
Hello, If I have a method of type public void myMethod() { } And I create a delegate void NewDelegate(void) What is the actual difference between the following calls and how they are handled by .NET and the OS NewDelegate del = NewDelegate(myMethod); del.BeginInvoke(null, null); and Thread t = new Thread(new ThreadStart(myMethod)); t.Start(); I know the delegate will use a thread from the thread pool, but will the thread t as well? What is the actual effect of one versus the other.
-
Hello, If I have a method of type public void myMethod() { } And I create a delegate void NewDelegate(void) What is the actual difference between the following calls and how they are handled by .NET and the OS NewDelegate del = NewDelegate(myMethod); del.BeginInvoke(null, null); and Thread t = new Thread(new ThreadStart(myMethod)); t.Start(); I know the delegate will use a thread from the thread pool, but will the thread t as well? What is the actual effect of one versus the other.
Calling BeginInvoke on a delegate makes the method execute on a thread from the CLR's thread pool. You can't get a refernce to that thread, so you don't have fine-grained control over it. :josh: My WPF Blog[^]
-
Calling BeginInvoke on a delegate makes the method execute on a thread from the CLR's thread pool. You can't get a refernce to that thread, so you don't have fine-grained control over it. :josh: My WPF Blog[^]
So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool? Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
-
So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool? Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
zaboboa wrote:
So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool?
A background thread will keep running unless you kill it or the UI thread is killed (i.e. close the app).
zaboboa wrote:
Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
I don't know. :josh: My WPF Blog[^]
-
Hello, If I have a method of type public void myMethod() { } And I create a delegate void NewDelegate(void) What is the actual difference between the following calls and how they are handled by .NET and the OS NewDelegate del = NewDelegate(myMethod); del.BeginInvoke(null, null); and Thread t = new Thread(new ThreadStart(myMethod)); t.Start(); I know the delegate will use a thread from the thread pool, but will the thread t as well? What is the actual effect of one versus the other.
Hello, First, as a general answer to your question about threading, take a look at this web-page[^]. It is long, but well worth the read. Jon Skeet (the author of that document) speaks of the difference between thread pool threads and threads created with
ThreadStart
. The main difference is that thread pool threads are already created and waiting for a job to execute. This means that it is faster to use a thread from the thread pool to do a task. That does not mean it is better, by any means. Mr. Skeet recommends (and I think this is wise) that you use threads from the thread pool if you have a short, quick task that you would like to be multithreaded, but that you should never use thread pool threads to perform long operations. Using threads from the thread pool for long operations is a bad idea because other applications need access to the threads as well. Also, the Framework itself uses threads from the threads pool, so it is bad idea to tie them up for a long time. If the Framework needs a thread and they are all involved in long tasks, then bad things can happen to your application. To sum up: if you have long tasks to be accomplished by other threads, useThreadStart
without a doubt. The extra time and resources it takes to create a new thread will not matter so much. If you have quick and easy tasks which need to be accomplished by other threads, use a thread pool thread. Hope that helps! Sincerely, Alexander Wiseman -
So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool? Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
It isn't just a reference issue, see my other post for details. Also look at this page from MSDN: About Thread Pools[^]. There is a section at the bottom entitled "Best Practices" that sums things up nicely. As regards your second question about apartment models, according to the following quote from another part of MSDN, you cannot use a thread pool thread in STA: "Each thread uses the default stack size, runs at the default priority, and is in the multithreaded apartment." Hope that helps! Sincerely, Alexander Wiseman
-
Hello, First, as a general answer to your question about threading, take a look at this web-page[^]. It is long, but well worth the read. Jon Skeet (the author of that document) speaks of the difference between thread pool threads and threads created with
ThreadStart
. The main difference is that thread pool threads are already created and waiting for a job to execute. This means that it is faster to use a thread from the thread pool to do a task. That does not mean it is better, by any means. Mr. Skeet recommends (and I think this is wise) that you use threads from the thread pool if you have a short, quick task that you would like to be multithreaded, but that you should never use thread pool threads to perform long operations. Using threads from the thread pool for long operations is a bad idea because other applications need access to the threads as well. Also, the Framework itself uses threads from the threads pool, so it is bad idea to tie them up for a long time. If the Framework needs a thread and they are all involved in long tasks, then bad things can happen to your application. To sum up: if you have long tasks to be accomplished by other threads, useThreadStart
without a doubt. The extra time and resources it takes to create a new thread will not matter so much. If you have quick and easy tasks which need to be accomplished by other threads, use a thread pool thread. Hope that helps! Sincerely, Alexander Wiseman