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
Post a Comment