Why Do We Have Different Statuses for a Task? [duplicate]
I refactored part of the code and found an interesting case. Imagine we have the following code:: var task1 = new Task(async () => { while (true) { Console.WriteLine("Hello from Task1"); await Task.Delay(TimeSpan.FromSeconds(5)); } }); task1.ContinueWith(_ => { Console.WriteLine("Hello from continue 1"); }); var task2 = new Task(() => { while (true) { Console.WriteLine("Hello from Task2"); Task.Delay(TimeSpan.FromSeconds(5)).Wait(); } }); task2.ContinueWith(_ => { Console.WriteLine("Hello from continue 2"); }); task1.Start(); task2.Start(); while (true) { await Task.Delay(TimeSpan.FromSeconds(7)); } So, what are we expecting here? Probably something like this: But in reality, we get the following result: Since tas1 get async lambda it will not be in the Running status: Does anyone have a clear explanation?
I refactored part of the code and found an interesting case. Imagine we have the following code::
var task1 = new Task(async () =>
{
while (true)
{
Console.WriteLine("Hello from Task1");
await Task.Delay(TimeSpan.FromSeconds(5));
}
});
task1.ContinueWith(_ =>
{
Console.WriteLine("Hello from continue 1");
});
var task2 = new Task(() =>
{
while (true)
{
Console.WriteLine("Hello from Task2");
Task.Delay(TimeSpan.FromSeconds(5)).Wait();
}
});
task2.ContinueWith(_ =>
{
Console.WriteLine("Hello from continue 2");
});
task1.Start();
task2.Start();
while (true)
{
await Task.Delay(TimeSpan.FromSeconds(7));
}
So, what are we expecting here? Probably something like this:
But in reality, we get the following result:
Since tas1 get async lambda it will not be in the Running status:
Does anyone have a clear explanation?