1

In my Winform VB.NET application I am checking some fields. In case that one particular field is equal to true I need to show message. Normally it would be like this:

If (myField) Then
     MessageBox.Show("Something is wrong", "Warning", MessageBoxButtons.OK)

    // continuing...
End If

This message must be shown modaly (user can return to main form only after clicking OK button). Problem is that I do not want the thread to wait for the clicking (just show the message and continue - do not wait for OK button).

My only idea is to show the message in background thread:

If (myField) Then
     Dim t As Thread = New Thread(AddressOf ShowMyMessage)
     t.Start()

     // continuing...
End If

Private Sub ShowMyMessage()
     MessageBox.Show("Something is wrong", "Warning", MessageBoxButtons.OK)
End Sub

But in this case the message is not shown modaly (user can return to main form and interact with it without clicking Ok button).

Any ideas?

DanielH
  • 953
  • 10
  • 30
  • Show the message box in UI thread. Why do you need a worker thread here? – Sriram Sakthivel Apr 16 '15 at 07:45
  • Because UI thread stops on the message. I have some code after it and it is executed only after closing the message – DanielH Apr 16 '15 at 07:50
  • That's what the meaning of the Modal dialog. You have the [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) ask the original question instead. – Sriram Sakthivel Apr 16 '15 at 07:51

1 Answers1

1

Your design is likely to be wrong if this is what you want to do, but for an exercise I wrote something that should achieve what you want.

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
    Dim thr As New Thread(Sub() ThreadTest())
    thr.Start()
End Sub

Private Sub ThreadTest()
    Debug.WriteLine("Started")
    Me.ShowMessageBox("Can't click on the main form now", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Debug.WriteLine("Thread continued")
End Sub

Public Sub ShowMessageBox(textToShow As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon)
    Me.BeginInvoke(Sub() MessageBox.Show(textToShow, caption, buttons, icon))
End Sub

When you run it you will see that the ThreadTest code carries on past the showing of the messagebox, but no interaction is allowed with the main form until the ok is clicked on the message box

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143