Sil*_*ent 3 c# multithreading threadpool
我必须在ThreadPool上运行一些代码作为单独的线程.
ThreadPool.QueueUserWorkItem(MyMethod,MyObjects);
Run Code Online (Sandbox Code Playgroud)
一旦MyMethod完成,我需要运行另一个方法MyMethod2.我怎样才能做到这一点?
这是一种方法:
ThreadPool.QueueUserWorkItem(o => { MyMethod(o); MyOtherMethod(); }, MyObjects);
Run Code Online (Sandbox Code Playgroud)
您可以使用任务并行库(TPL):
Task.Factory.StartNew(() =>
{
//your method call(s) here
})
.ContinueWith((task) =>
{
//your on completion code here
});
Run Code Online (Sandbox Code Playgroud)