Resolved Messagebox from background thread but tied to UI

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
Currently I have a background worker doing a bunch of stuff and if a condition is met, it throws up a MessageBox for the user to pick from an the background thread pauses like I need it to, however most of the time the Messagebox shows up behind the main form (which is on the primary app thread, the UI thread) and I'd like the BW code to pause until it's acknowlegded and I also want the MessageBox to be owned by the main form (so it's always displayed in front and you can't get to the main form until you acknowledge the MessageBox.

Later I'll be changing the MessageBox to be a modal form that matches the app's theme (I'll be opening this form using .ShowDialog()) should I go ahead and make that form now?
 
You can use Control.Invoke to call the dialog from UI thread, or method that calls dialog if you wish. When Invoke returns (ie dialog dismissed) the worker continues.
 
I dunno if that would work though, however I did end up learning about: Threading.AutoResetEvent
VB.NET:
Private m_MyVariable As Boolean
Private ThreadSignal As New Threading.AutoResetEvent(False)

Worker_DoWork event:
Call ShowDialogSub
ThreadSignal.WaitOne(-1, True) 
If m_MyVariable Then ....

ShowDialogSub:
Using ThatForm As New SomeForm
    m_MyVariable = ThatForm.ShowDialog(Me) = DialogResult.Yes
    ThreadSignal.Set()
End Using
 
Back
Top