我正在创建一个任务,然后分配一个等待任务完成的线程,然后修改 UI。
string txt = txtHelloMessage.Text;
HelloTask = Task<string>.Factory.StartNew(
() =>
{
string msg = client.SayHello(txt);
return msg;
}
);
new Thread(
() =>
{
while (true)// waiting for completion, I think that this is wrong
{
if (HelloTask.IsCompleted)
{
this.Dispatcher.Invoke((Action)delegate() { txtHelloMessage.Text = HelloTask.Result; });
break;
}
}
}
).Start();
这是好习惯吗?