Very simple thread question

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hi everybody,
I just use this simple code to run a process on a safe-thread:
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
        thRead.Start()
    End Sub

    Private Sub myThread(ByVal o As Object)
        '...my process...
        MessageBox.Show("hi")
    End Sub
There is a problem here that this Message Box will not be shown as MODAL.
I really need that this message box appear in MODAL state.
Usually a message box will be shown as modal, so how should I fix this probelm?
Thank you all:)
 
Marshalling with Invoke

I described in this thread, you should place the messagebox code in a method which is called using Invoke, to marshal it to the UI thread, like so:

Visual Basic:
Private Delegate Sub MessageBoxHandler(ByVal message As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
    thRead.Start()
End Sub

Private Sub ShowMessageBox(ByVal message As String)
    MessageBox.Show(message)
End Sub

Private Sub myThread(ByVal o As Object)
    '...my process...
    
    'Call ShowMessageBox on UI thread
    Me.Invoke(New MessageBoxHandler(AddressOf ShowMessageBox), "hi")

End Sub

Good luck :cool:
 
Thank you so much for your help, just wanted to know how to pass 2 more parameters for MsgBox title and style?
 
I have changed my code to this:
Visual Basic:
    Private Delegate Sub MessageBoxHandler(ByVal Message As String, ByVal Caption As String, ByVal Style As String)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
        thRead.Start()
    End Sub
    Private Sub ShowMessageBox(ByVal message As String, ByVal caption As String, ByVal style As String)
        Select Case style
            Case "Exclamation"
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Case "Information"
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Select
    End Sub
    Private Sub myThread(ByVal o As Object)
        '...my process...
        'Call ShowMessageBox on UI thread
        Me.Invoke(New MessageBoxHandler(AddressOf ShowMessageBox), "hi", "ed", "Exclamation")
    End Sub
But for MessageBox.Style I am using a string variable.
May I use another type of variable to avoid using select case like this?
I think this code is not very well.
 
Invoke within the method

I feel I should elaborate a little. The code Me.Invoke(...) causes the call to be marshalled to the UI thread, but as you will agree it would be quite clumsy to have to put this everywhere you wished to display a message box. The answer is to put the invoke call within the method itself, so that it can be called from anywhere like any other method, and will marshall the call to the UI thread as needed. Combined with using a parameter of type MessageBoxIcon, the code might read:

Visual Basic:
    Private Delegate Sub MessageBoxHandler(ByVal message As String, ByVal caption As String, ByVal icon As MessageBoxIcon)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thRead As New Thread(New ParameterizedThreadStart(AddressOf myThread))
        thRead.Start()
    End Sub

    Private Sub ShowMessageBox(ByVal message As String, ByVal caption As String, ByVal icon As MessageBoxIcon)

        'Check if marshalling is required
        If Me.InvokeRequired Then
            'Invoke to marshall to UI thread
            Me.Invoke(New MessageBoxHandler(AddressOf ShowMessageBox), message, caption, icon)
        Else
            'Already on UI thread, show the message box
            MessageBox.Show(message, caption, MessageBoxButtons.OK, icon)
        End If
    End Sub
    Private Sub myThread(ByVal o As Object)
        '...my process...
        'Call ShowMessageBox on UI thread
        ShowMessageBox("hi", "ed", MessageBoxIcon.Exclamation)
    End Sub

Good luck :cool:
 
Back
Top