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?

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top