multithreading - Improving Task code to achieve async await in c# 4.5 -



multithreading - Improving Task code to achieve async await in c# 4.5 -

this simple code,i creating task , assign thread waits task completed , modify ui.

string txt = txthellomessage.text; hellotask = task<string>.factory.startnew( () => { string msg = client.sayhello(txt); homecoming msg; } ); new thread( () => { while (true)// waiting completion, think wrong { if (hellotask.iscompleted) { this.dispatcher.invoke((action)delegate() { txthellomessage.text = hellotask.result; }); break; } } } ).start();

is practice? there improve way accomplish ?

i using c#4.5

no, it's not practice.

you're using heavy , expensive thread handle outcome of lightweight (cheap) task.

in case allow task invoke(), or utilize continuation.

but code looks artificial, maybe post closer real problem.

c# multithreading task

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -