c# - Call method till it returns value -


i want call method till returns value in wpf app.

void btndecode_click(object sender, routedeventargs e) {     var task = new task(task);     task.start();     task.wait(); }  async void task() {     task<object> result= decodedresult((bitmapsource)imagebarcode.source);     object = await result;     txtbarcodecontent.text = i.tostring(); }  async task<object> decodedresult(bitmapsource rendertargetbitmap) {     var reader = new barcodereader();     txtbarcodecontent.text = "reading";      return reader.decode(rendertargetbitmap); } 

but throws me error on task.start();

"additional information: calling thread cannot access object because different thread owns it."

why can't access , why thread own it?

whenever update ui elements thread other main thread, need use:

this.dispatcher.invoke((action)(() => {        ...// code here. })); 

you can use control.dispatcher.checkaccess() check whether current thread owns control. if own it, code looks normal. otherwise, use above pattern.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -