How to use Asynchronous MessageBox?

CathInfo

Member
Joined
Jun 5, 2008
Messages
9
Programming Experience
5-10
Who hasn't used the venerable

VB.NET:
MessageBox.Show("Hello, " + Name, "Messagebox Title", MessageBoxButtons.OK)

in their programs countless times. But I am having a small problem with the "stop the whole world" nature of this function call. That is, I have database connections that need to be "kept alive" for the program to function properly -- and if the user leaves such a MessageBox on the screen for more than 10 seconds before clicking "OK", the connections start getting dropped.

How can I display an asynchronous MessageBox -- one that is in a different thread?

Thanks,

Matthew
 
MessageBox.Show is really just a form that is displayed modally as with ShowDialog. Just create your own form with a Label and a Button and Show() it.
 
I went ahead and tried something like that. It displays the correct message/title, but the whole program locks up (it stops responding).
I know there's something stupid I'm doing, but I don't know what it is. I haven't done much with threads up till now.

NOTE: This isn't the entire class, because I created a "new Windows Form" using the VB.Net IDE, and added it to my project. Anything I added in the "Design view" doesn't show up here.

Thanks in advance for any help,

Matthew


VB.NET:
Public Class matt_Message

    Public Sub New(ByVal strMyMessage As String, Optional ByVal strTitle As String = "Message")
        InitializeComponent()
        Me.Text = strTitle
        Me.lblMessage.Text = strMyMessage
        Me.CenterToParent()
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Visible = True
        Me.BringToFront()
    End Sub

    Public Sub Run()
        Me.ShowDialog()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Close()
    End Sub
End Class
 
Since you obviously do the opposite of what people tell you, I have to advice you to use ShowDialog method and not Show method as I said in previous post.
 
Actually, I started with Show().

The program ended with an exception -- "Cross-thread operation not valid: Control 'matt_Message' accessed from a thread other than the thread it was created on."

I'm just wondering what I'm doing wrong.

Sorry for the confusion,

Matthew
 
Last edited:
So how did you manage to "accessed from a thread other than the thread it was created on"? No matter which thread this form was created on I don't see how you would be accessing it again? And if you would the error message box is proposing the solution for you in that case, see the Troubleshooting tips box "how to make cross-thread calls to Windows Forms controls".
 
I looked at the MSDN help for that error -- apparently VB.net studio plays it safe and complains whenever you do something that isn't "thread-safe" -- especially in debug mode.

I don't think I'd ever have a problem though, whatever they say.

All I'm doing is creating a thread to display a windows form with a message, which stays there until the user presses the "OK" button, then it closes. What's so unsafe about that?

Moreover, they gave some "code examples" which demonstrated safe and unsafe methods of using threads. But try as I might, I couldn't tell any difference between the two! Very confusing at best.

At least the problem was solved.

Thanks again,

Matthew
 
This answers your question:
VB.NET:
Private Sub Button1_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Button1.Click
    Dim t As New Threading.Thread(AddressOf ShowMessageBox)

    t.Start("This message is displayed in a worker thread, " & _
            "therefore the dialogue is not modal with respect to the main thread.")
End Sub

Private Sub ShowMessageBox(ByVal message As Object)
    MessageBox.Show(message.ToString())
End Sub
but it achieves nothing over what JohnH suggested in the very first reply though.
 
Back
Top