.Net Threading — BeginInvoke uses the thread pool

For a while now I’ve been curi­ous as to whether the CLR uses the Thread­Pool to exe­cute a del­e­gate when Begin­In­voke is called:

private void InvokeFunc(Func<int> func)
    func.BeginInvoke(null, null); // does this execute on a threadpool thread?

Whilst com­mon sense dic­tates that this must sure­ly be true, I couldn’t be cer­tain since I haven’t man­aged to find any con­fir­ma­tion in the doc­u­men­ta­tions.

Thanks to Jon Skeet and Jeff Ster­nal who pro­vid­ed the answer to my ques­tion and a link to the MSDN arti­cle which con­firms it:

If the Begin­In­voke method is called, the com­mon lan­guage run­time (CLR) queues the request and returns imme­di­ate­ly to the caller. The tar­get method is called asyn­chro­nous­ly on a thread from the thread pool.

This of course, means that if your del­e­gate is like­ly to take a while to exe­cute you should not call Begin­In­voke on the del­e­gate to avoid block­ing the Thread­Pool threads, instead you could cre­ate a new thread or use a Smart­Thread­Pool instance. I’ve dis­cussed these aspect in more detail here and here if you’re inter­est­ed.


MSDN – Asyn­chro­nous Pro­gram­ming using Del­e­gates

Stack­Over­flow Ques­tion – Does Func.BeginInvoke use the Thread­Pool

Liked this post? Why not support me on Patreon and help me get rid of the ads!