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?
No, it’s not good practice.
You’re using a very heavy and expensive Thread to handle the outcome of a lightweight (cheap) Task.
In this case you could just let the Task itself do the Invoke(), or use a continuation.
But the code looks very artificial, maybe post something closer to your real problem.