在C#中,使用委托异步地执行某些工作(调用BeginInvoke())和使用ThreadPool线程之间有任何区别,如下所示
public void asynchronousWork(object num)
{
//asynchronous work to be done
Console.WriteLine(num);
}
public void test()
{
Action<object> myCustomDelegate = this.asynchronousWork;
int x = 7;
//Using Delegate
myCustomDelegate.BeginInvoke(7, null, null);
//Using Threadpool
ThreadPool.QueueUserWorkItem(new WaitCallback(asynchronousWork), 7);
Thread.Sleep(2000);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
BeginInvoke确保线程池中的线程用于执行异步代码,所以有什么区别吗?