Wednesday, August 26, 2009

A quick tip for error "Cross-thread operation not valid..."

I was helping to diagnose a threading error that goes like Cross-thread operation not valid... This typically happens when the UI thread is being accessed by another thread. Well, there might be solutions like to use Invoke delegation, here I would like to give a tip built on the background worker helper I wrote few weeks ago.

The tip is to use the progress handler (ProgressChangedMethod in my prior example).

For instance, when you want to refresh the UI count... instead of triggering an event (or a call) from the DoWorkMethod, fire up a ProgressChangedMethod call and from there either fire up an event or call your UI methods.

Love coding!

No comments: