我在WinForm上有这段代码:
private void buttonStart_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => ThreadFunction())
.ContinueWith(t => ThreadFunctionComplete()
, CancellationToken.None
, TaskContinuationOptions.None
, TaskScheduler.FromCurrentSynchronizationContext());
ThreadFinished();
}
我想在ThreadFinished()
完成后才开始ThreadFunctionComplete()
。好吧,我可以将ThreadFinished()
放在ThreadFunctionComplete()
的末尾,但对于该过程的某些工作流程,我不会。可能吗?还有一种等待/锁定?
答案 0 :(得分:4)
基本上,您只需拨打ContinueWith
两次:
Task.Factory.StartNew(....)
.ContinueWith(...)
.ContinueWith(...)
答案 1 :(得分:1)
您可以使用Wait()但它会阻止当前线程......
Task.Factory.StartNew(...).Wait();
或者添加另一个ContinueWith()
Task.Factory.StartNew(...).ContinueWith(t => ThreadFinished());