按顺序运行任务
本文关键字:任务 运行 顺序 | 更新日期: 2023-09-27 18:02:46
我有五个windows服务。我按顺序运行服务。
if(a_Condition)
{
services[0] = new Processing_0();
// blah blah
}
if(b_Condition)
{
service[1] = new Processing_1();
// blah blah
}
if(c_Condition)
{
service[2] = new Processing_2();
// blah blah
}
if(d_Condition)
{
service[3] = new Processing_3();
// blah blah
}
if(e_Condition)
{
service[4] = new Processing_4();
// blah blah
}
由于某种原因,我想使用task,代码是:
Task.StartNew(() => {
if (a_Condition) { var x = new Processing_0(); ... }
})
.ContinueWith(() => {
if (b_Condition) { var x = new Processing_1(); ... }
})
.ContinueWith(() => {
if (c_Condition) { var x = new Processing_2(); ... }
})
.ContinueWith(() => {
if (d_Condition) { var x = new Processing_3(); ... }
})
.ContinueWith(() => {
if (e_Condition) { var x = new Processing_4(); ... }
});
可以吗?
很好!
在https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/chaining-tasks-by-using-continuation-tasks查看MSDN关于Continuations的文章