Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everybody,

I just use this simple code to run a process on a safe-thread:

   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:)

Posted

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

I have changed my code to this:

   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.

Posted

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:

 

    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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...