Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Difference between Thread.Start() and Delegate.BeginInvoke()

Difference between Thread.Start() and Delegate.BeginInvoke()

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaboboa
    wrote on last edited by
    #1

    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.

    J A 2 Replies Last reply
    0
    • Z zaboboa

      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.

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      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[^]

      Z 1 Reply Last reply
      0
      • J Josh Smith

        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[^]

        Z Offline
        Z Offline
        zaboboa
        wrote on last edited by
        #3

        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?

        J A 2 Replies Last reply
        0
        • Z zaboboa

          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?

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          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[^]

          1 Reply Last reply
          0
          • Z zaboboa

            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.

            A Offline
            A Offline
            Alexander Wiseman
            wrote on last edited by
            #5

            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, use ThreadStart 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

            Z 1 Reply Last reply
            0
            • Z zaboboa

              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?

              A Offline
              A Offline
              Alexander Wiseman
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A 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, use ThreadStart 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

                Z Offline
                Z Offline
                zaboboa
                wrote on last edited by
                #7

                Thank you very much guys for all your replies. It was very helpfull.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups