难道Func键< T> .BeginInvoke使用线程池? [英] Does Func<T>.BeginInvoke use the ThreadPool?

查看:119
本文介绍了难道Func键< T> .BeginInvoke使用线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你调用BeginInvoke方法上的Func键代表(或代表行动为此事)在C#中,并在运行时使用线程池或产生新线程?

When you call the BeginInvoke method on a Func delegates (or the Action delegates for that matter) in C#, does the runtime use the ThreadPool or spawn a new thread?

我几乎可以肯定,它会使用线程池因为这会是合乎逻辑的事情,但会AP preciate,如果有人可以证实这一点。

I'm almost certain that it'll use the ThreadPool as that'd be the logical thing to do but would appreciate it if someone could confirm this.

谢谢

推荐答案

它使用线程池,绝对。

我炸飞,如果我能找到反正那个文档,你要知道...... 这个MSDN文章表示,任何的回调的你指定将在线程池中的线程中执行...

I'm blowed if I can find that documented anyway, mind you... this MSDN article indicates that any callback you specify will be executed on a thread-pool thread...

下面是一些code,以确认它 - 但当然,这并不确定,它的的保证的发生这样...

Here's some code to confirm it - but of course that doesn't confirm that it's guaranteed to happen that way...

using System;
using System.Threading;

public class Test
{
    static void Main()
    {
        Action x = () => 
            Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);

        x(); // Synchronous; prints False
        x.BeginInvoke(null, null); // On the thread-pool thread; prints True
        Thread.Sleep(500); // Let the previous call finish
    }
}

编辑:由于联杰夫下面,这个MSDN文章证实了这一点:

如果BeginInvoke方法被调用时,   公共语言运行库(CLR)   排队请求,并返回   立即给调用者。目标   方法是异步调用上   从线程池中的线程。

If the BeginInvoke method is called, the common language runtime (CLR) queues the request and returns immediately to the caller. The target method is called asynchronously on a thread from the thread pool.

这篇关于难道Func键< T> .BeginInvoke使用线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆