Reputation:
I have on form (ui thread) and a backgroundworker thread. I am trying to check whether a radio button in the main ui thread is checked or not inside the backgroundworker thread. How would I go upon comparing the bool type inside an if statement that is inside my backgroundworker's dowork event?
Note: I am using WPF.
private void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
if () // if (rdbCreditCard.Checked == true) {} etc. etc.
{
}
}
Upvotes: 2
Views: 192
Reputation: 24208
You might want to consider not accessing the checkbox from the background thread at all. Doing so will cost a couple of context switches and the serializing of the request through a dispatcher or a delegate.
Instead, you could add an event handler to the checkbox that executes in the UI thread, and records the checkbox state for use by the worker thread. If you only need the checkbox's state when the worker starts, you could pass it as a thread parameter from the UI thread, avoiding the event handler and state.
Upvotes: 3
Reputation: 9489
If you are using WPF, you use the UI thread's dispatcher and invoke the method on the ui thread. If using winforms, you make a delegate on the ui thread to run the method.
Upvotes: 2