1

let's say we have some simple code like this :

private static void Main()
{
    Console.WriteLine("Main thread {0}\n", Thread.CurrentThread.ManagedThreadId);

    Action asyncCaller1 = () => LongPerfomingTask(5);
    Action asyncCaller2 = () => LongPerfomingTask(3);

    var asyncResult1 = asyncCaller1.BeginInvoke(null, null);
    var asyncResult2 = asyncCaller2.BeginInvoke(null, null);

    asyncResult1.AsyncWaitHandle.WaitOne();
    asyncResult2.AsyncWaitHandle.WaitOne();

    Console.WriteLine("Done");
}

private static void LongPerfomringTask(int seconds)
{
    Thread.Sleep(TimeSpan.FromSeconds(seconds));

    Console.WriteLine("Thread {0} finished execution", Thread.CurrentThread.ManagedThreadId);
}

Delegate.BeginInvoke() does not create a thread, It's executing code in a caller's thread when it is in idle state, right?So, why the output of this sample application is like this :

Main thread 1

Thread 4 finished execution
Thread 3 finished execution
Done
illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84

2 Answers2

6

No, Delegate.BeginInvoke uses the thread pool. Always. There's no concept of "executing in the caller's thread when it's idle" unless you're thinking of adding tasks to a UI message queue... were you getting confused with Control.BeginInvoke / Dispatcher.BeginInvoke?

In this case you've got a console app - there's no message pumping going on to start with.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I got confused by some post about delegates on SO (Accepted answer was like 'Delegate.BeginInvoke() does not create a separate thread - the code is being executed in caller's thread'). I always thought BeginInvoke() method take a thread from a Thread Pool an executes code there, now I am sure. Thanks. BTW, I found Control.BeginInvoke()/Dispatcher.BeginInvoke()'s names very strange, that's because they actually do not create threads. I think these methods are named really confusing – illegal-immigrant Apr 05 '11 at 14:50
  • @taras: Feel free to point me to that thread so I can correct others :) – Jon Skeet Apr 05 '11 at 14:51
  • http://stackoverflow.com/questions/1909839/invoke-and-begininvoke I probably misunderstood that question, I double-checked it after your answer, and realized that everything seems to be just fine. I read a code sample not very carefully (Searched about DynamicInvokes on SO, so I thought I was pointed to 'Delegates thread'), It's WF example, not plain delegate method calls – illegal-immigrant Apr 05 '11 at 15:02
  • @taras: Right - that's definitely talking about the UI version. It's a shame they're named confusingly :( – Jon Skeet Apr 05 '11 at 15:05
  • 1
    @anishMarokey: I use Visual Studio for serious work, and a plain text editor for short examples in SO and blog posts. – Jon Skeet Apr 05 '11 at 16:02
  • @Joh Skeet and @all SO: i asked the same question here http://meta.stackoverflow.com/questions/86080/how-jon-skeet-marc-gravell-and-other-big-so-persons-remember-the-code – anishMarokey Apr 05 '11 at 17:19
  • @Jon Skeet remember application.idle event – peter Aug 21 '13 at 17:42
0

@taras.roshko: Here's a resource to boost your understanding of ThreadPool: Chapter on Threading

GregC
  • 7,737
  • 2
  • 53
  • 67