the calling thread cannot access wpf win form c# multi threading
If you receive this error:
The calling thread cannot access this object because a different thread owns it
It is a simple fix to add in some common multi-threading invocation mechanisms.
WPF:
//with delegate declared public delegate mydelegate; this.Dispatcher.Invoke(new mydelegate(funcname)); this.Dispatcher.Invoke((mydelegate)delegate() { funcname(); }); //does not require delegate declared, i usually prefer this approach but there are situations for either this.Dispatcher.Invoke((Action)delegate() { funcname(); }); this.Dispatcher.Invoke((Action)(()=>{ funcname();}));
WinForms:
this.Invoke(funcname, params); this.BeginInvoke(funcname, params);
Posted on November 5, 2012, in Programming & Development and tagged access, async, begininvoke, calling, cannot, invoke, multi, multicore, multithreading, owner, owns, parallel, this, thread, threading, wpf. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0