Task.ContinueWith 不耽误
Task.ContinueWith not delaying
为什么操作1和2同时执行?而不是相隔 30 秒?
运行 在 ASP.NET 上的 IIS 下
Task t = Task.Factory.StartNew(() =>
{
Console.WriteLine("Operation 1");
}
).ContinueWith(async task =>
{
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
}
).ContinueWith(
task =>
{
Console.WriteLine("Operation 2");
}
);
t.Wait();
首先ContinueWith
returnsTask<Task>
,然后你需要.Unwrap
它:
Task t = Task.Factory.StartNew(() => {
Console.WriteLine("Operation 1");
})
.ContinueWith(async task => {
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
})
.Unwrap()
.ContinueWith(task => {
Console.WriteLine("Operation 2");
});
t.Wait();
为了更深入的理解,您可以检查 ContinueWith
的所有实现:
在第一个 ContinueWith
中使用,在第二个中使用。
还要检查async task => { ...
,你需要明白它returns Task
.
最后说明一下(如果不是理解ContinueWith
的问题),最好用await
:
Task t = Task.Factory.StartNew(async () => {
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");
});
t.Wait();
Why do operations 1 and 2 execute at the same time? and not 30 seconds apart?
就此而言,简短的回答是 ContinueWith
does not understand async
. Neither does StartNew
。您不应该使用 ContinueWith
或 StartNew
.
Instead of StartNew
, use Task.Run
,而不是 ContinueWith
,使用 await
:
Task t = Task.Run(async () =>
{
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");
});
t.Wait();
Running under IIS on ASP.NET
因为你在 ASP.NET,you shouldn't be using Task.Run
, either:
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");
为什么操作1和2同时执行?而不是相隔 30 秒?
运行 在 ASP.NET 上的 IIS 下
Task t = Task.Factory.StartNew(() =>
{
Console.WriteLine("Operation 1");
}
).ContinueWith(async task =>
{
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
}
).ContinueWith(
task =>
{
Console.WriteLine("Operation 2");
}
);
t.Wait();
首先ContinueWith
returnsTask<Task>
,然后你需要.Unwrap
它:
Task t = Task.Factory.StartNew(() => {
Console.WriteLine("Operation 1");
})
.ContinueWith(async task => {
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
})
.Unwrap()
.ContinueWith(task => {
Console.WriteLine("Operation 2");
});
t.Wait();
为了更深入的理解,您可以检查 ContinueWith
的所有实现:
在第一个 ContinueWith
中使用,在第二个中使用。
还要检查async task => { ...
,你需要明白它returns Task
.
最后说明一下(如果不是理解ContinueWith
的问题),最好用await
:
Task t = Task.Factory.StartNew(async () => {
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");
});
t.Wait();
Why do operations 1 and 2 execute at the same time? and not 30 seconds apart?
就此而言,简短的回答是 ContinueWith
does not understand async
. Neither does StartNew
。您不应该使用 ContinueWith
或 StartNew
.
Instead of StartNew
, use Task.Run
,而不是 ContinueWith
,使用 await
:
Task t = Task.Run(async () =>
{
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");
});
t.Wait();
Running under IIS on ASP.NET
因为你在 ASP.NET,you shouldn't be using Task.Run
, either:
Console.WriteLine("Operation 1");
log("sleeping during turn on");
await Task.Delay(25000);
log("finished awaiting");
Thread.Sleep(5000);
log("finished sleeping");
Console.WriteLine("Operation 2");