简体   繁体   English

阻止Task.Run方法直到完成

[英]Block Task.Run method until done

I've got some methods that I need to run, one of them should run as a different Thread so I use a Task.Run with lambda, though I want the next method to start just after the Task finishes. 我有一些需要运行的方法,其中一个应作为其他线程运行,因此我使用Task.Run和lambda一起运行,尽管我希望下一个方法在Task完成后立即开始。

for example I want LastJob() will start after MoreWork() is done: 例如,我希望LastJob()将在MoreWork()完成后开始:

public void DoSomeWork()
{
    Task.Run(() => MoreWork());
    LastJob();
}

you can use the async and await key words link 您可以使用asyncawait关键字链接

public async void DoSomeWork()
{
    await Task.Run(() => MoreWork());
    LastJob();
}

Coy could use the ContinueWith method, like this: Coy可以使用ContinueWith方法,如下所示:

public void DoSomeWork()
{
    var task = Task.Run(() => MoreWork());
    task.ContinueWith(() => LastJob()) ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM