Func<T>.BeginInvoke 使用线程池吗?

6

免费vpn vpn下载 本文介绍了Func<T>.BeginInvoke 使用线程池吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn vpn free

问题描述

当您在 C# 中调用 Func 委托(或相关的 Action 委托)上的 BeginInvoke 方法时,运行时是使用 vpn free ThreadPool 还是生成新线程?

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

我几乎可以肯定它会使用 ThreadPool,因为这是合乎逻辑的做法,但如果有人能证实这一点,我将不胜感激.

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

谢谢,

推荐答案

肯定是用线程池的.

如果我能找到文档,我会感到震惊,请注意... 这篇 MSDN 文章 表明您指定的任何 回调 都将在线程池线程上执行...

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

这里有一些代码可以确认它 vpn free - 但当然这并不能确认它保证会以这种方式发生......

Here's some code to 免费vpn confirm it - vpn下载 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()
   vpn free  {
        Action free vpn vpn free x = () => 
    免费vpn         Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);

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

如下面的 Jeff 所链接,这篇 MSDN 文章 证实它:

As linked by Jeff below, this MSDN article confirms it:

如果调用了 BeginInvoke 方法,公共语言运行时 (CLR)将请求排队并返回立即给来电者.目标方法在 a free vpn 上异步调用线程池中的线程.

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

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird vpn下载 behavior free vpn in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to 免费vpn vpn free store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in free vpn the background)?(代表如何工作(在后台)?)...
免费vpn下载 2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用? vpn free
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14