一旦ThreadPool线程完成其工作,就调用一个方法

Sil*_*ent 3 c# multithreading threadpool

我必须在ThreadPool上运行一些代码作为单独的线程.

ThreadPool.QueueUserWorkItem(MyMethod,MyObjects);
Run Code Online (Sandbox Code Playgroud)

一旦MyMethod完成,我需要运行另一个方法MyMethod2.我怎样才能做到这一点?

Tho*_*que 5

这是一种方法:

ThreadPool.QueueUserWorkItem(o => { MyMethod(o); MyOtherMethod(); }, MyObjects);
Run Code Online (Sandbox Code Playgroud)


Bro*_*ass 5

您可以使用任务并行库(TPL):

Task.Factory.StartNew(() =>
    {
        //your method call(s) here
    })
    .ContinueWith((task) =>
    {
       //your on completion code here
    });
Run Code Online (Sandbox Code Playgroud)