Améliorer le code de la tâche pour réaliser Async attendre
-
12-11-2019 - |
Question
Je crée une tâche, puis j'attribue un thread qui attend la tâche à terminer, puis je modifie l'interface utilisateur.
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();
Est-ce que cette bonne pratique?
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow