ADO DOT NET Posted February 8, 2007 Posted February 8, 2007 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:) Quote
MrPaul Posted February 8, 2007 Posted February 8, 2007 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: Quote Never trouble another for what you can do for yourself.
ADO DOT NET Posted February 8, 2007 Author Posted February 8, 2007 Thank you so much for your help, just wanted to know how to pass 2 more parameters for MsgBox title and style? Quote
ADO DOT NET Posted February 9, 2007 Author Posted February 9, 2007 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. Quote
Administrators PlausiblyDamp Posted February 9, 2007 Administrators Posted February 9, 2007 Why not just use a parameter of type MessageBoxIcon? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrPaul Posted February 11, 2007 Posted February 11, 2007 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: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.