.Net 中的多任务
MultiTask in .Net
如代码注释询问。我不知道为什么等待“continuation.Result”的主线程在打印出它的值之前有值。
static void Main(string[] args)
{
Task<string> antecedent = Task.Delay(5000).ContinueWith<string>(x =>
{
Console.WriteLine("return in task1 {0}", Thread.CurrentThread.ManagedThreadId);
return DateTime.Today.ToShortDateString();
});
Task<string> continuation = antecedent.ContinueWith(x =>
{
x.Wait(1000);
Console.WriteLine("return in task2 {0}{1}",Thread.CurrentThread.ManagedThreadId,x.Status);
return "Today is " + antecedent.Result;
});
Console.WriteLine("this will print before the result");
Console.WriteLine(continuation.Result); //why this waiting ?
Console.ReadKey();
}
I don't know why the main thread waiting for "continuation.Result" had value before print out its value.
您还希望它做什么?它无法在之前打印一个值它有它。
Task.Result 内部等待任务:
Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete
如代码注释询问。我不知道为什么等待“continuation.Result”的主线程在打印出它的值之前有值。
static void Main(string[] args)
{
Task<string> antecedent = Task.Delay(5000).ContinueWith<string>(x =>
{
Console.WriteLine("return in task1 {0}", Thread.CurrentThread.ManagedThreadId);
return DateTime.Today.ToShortDateString();
});
Task<string> continuation = antecedent.ContinueWith(x =>
{
x.Wait(1000);
Console.WriteLine("return in task2 {0}{1}",Thread.CurrentThread.ManagedThreadId,x.Status);
return "Today is " + antecedent.Result;
});
Console.WriteLine("this will print before the result");
Console.WriteLine(continuation.Result); //why this waiting ?
Console.ReadKey();
}
I don't know why the main thread waiting for "continuation.Result" had value before print out its value.
您还希望它做什么?它无法在之前打印一个值它有它。
Task.Result 内部等待任务:
Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete