-1

I run a time consuming process using background worker and i have disable all other buttons while the above said process is in execution. but when i try to disable other buttons,I get cross thread exception not valid error. how can I solve this?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Are you running a windows forms app or a wpf app? – hcb Feb 19 '13 at 11:45
  • Have you tried pasting the exception in a search engine and take a look at `(Begin)Invoke()`? Possible duplicate of [Cross thread UI component call](http://stackoverflow.com/questions/1395167/cross-thread-ui-component-call) – CodeCaster Feb 19 '13 at 11:45
  • if WPF: Dispatcher.BeginInvoke is what you're looking for. But anyways, why on EARTH are you using a BkgdWkr? – It'sNotALie. Feb 19 '13 at 11:48
  • You get the exception because you are trying to access a button on the main thread inside a background thread. Try to use Invoke. – Jordy van Eijk Feb 19 '13 at 11:49

1 Answers1

1

Disable your buttons before starting background worker.

DisableButton(); // runs on UI thread
backgroundWorker.RunWorkerAsync();

Then use RunWorkerCompleted event handler to enable buttons:

void backgroundWorker_RunWorkerCompleted(object s, RunWorkerCompletedEventArgs e)
{
    EnableButtons(); // runs on UI thread
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459