110,534
社区成员
发帖
与我相关
我的任务
分享
class Program
{
string s = "";
static void Main(string[] args)
{
Program p = new Program();
Task<string> task = Task.Run<string>(() => p.DoSomeThing());
Console.WriteLine("主线程继续执行");
Console.WriteLine(task.IsCompleted); //任务未完成,输出False
Console.ReadKey();
}
private string DoSomeThing()
{
Thread.Sleep(5000); //模拟耗时计算
s = "我好想熊俊";
Console.WriteLine(s); //5秒后输出
return s;
}
}
Task<string> task = Task.Run<string>(() => p.DoSomeThing());
task.ContinueWith((t) =>
{
Console.WriteLine("任务完成。结果是:" +t.Result);
});
//...