Improving Task code to achieve async await
-
12-11-2019 - |
Вопрос
I am creating a task and then assign a thread that waits for the task to be completed and then modify the 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();
Is this good practice?
Нет правильного решения
Не связан с StackOverflow