[RESOLVED]Threading and messageboxes
I have a mdi application. My child forms are using threading every so often. In the middle of the thread I am required to display a messagebox (the standard messagebox.show). But for some reason the messagebox can be moved behind the parent by clicking away from the messagebox. How can I stop this from happening?
Thank
Re: Threading and messageboxes
When you display a modal dialogue, which a MessageBox is, on a thread other than the one on which the calling object was created, it does not behave modally in relation to the caller. You would need to use a delegate to invoke a method on the main thread and display the MessageBox from that method, e.g.:
VB Code:
Private Delegate Sub ShowMessageDelegate(ByVal message As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(AddressOf ThreadStart)
t.Start()
End Sub
Private Sub ThreadStart()
Me.Invoke(New ShowMessageDelegate(AddressOf ShowMessage), New Object() {"Hello"})
End Sub
Private Sub ShowMessage(ByVal message As String)
MessageBox.Show(message)
End Sub
Re: [RESOLVED]Threading and messageboxes
Re: [RESOLVED]Threading and messageboxes
I made a form and used jmcilhinney's code on a button click *As above
anyways .. the messagebox is now modal .... Now i have a question .....
let's say that button has to save and reload and do a bunch of things ..... can I make it so that instead of using the messagebox ... it displays a form that is showing "Please Wait" or whatever else i want to put on there ... and make that modal .... ****While that is showing ... it must execute the rest of the code below the button ...
Is this at all possible?
Thanks
Cranzy
Re: [RESOLVED]Threading and messageboxes
Quote:
Originally Posted by Cranzy
I made a form and used jmcilhinney's code on a button click *As above
anyways .. the messagebox is now modal .... Now i have a question .....
let's say that button has to save and reload and do a bunch of things ..... can I make it so that instead of using the messagebox ... it displays a form that is showing "Please Wait" or whatever else i want to put on there ... and make that modal .... ****While that is showing ... it must execute the rest of the code below the button ...
Is this at all possible?
Thanks
Cranzy
In the code I have provided, the MessageBox is displayed from, and is therefore modal to, the main thread, which means that the worker thread is still available to do work. You could display some other form, that perhaps didn't have an OK button, modally if you wanted to. The issue would be closing that form again when the worker thread was finished. It shouldn't be done from the worker thread because it doesn't own the window handle, and it can't be done from the main thread because it is blocking waiting for the call to ShowDialog to return. I think in that case the best course of action would be to open the modal form with the "Please Wait" message from the main thread and then start the worker thread from that form. That way you can easily close that form when the worker thread terminates. If you want to disable the Close button on the title bar of that form, take a look at my submission in the CodeBank that is just for that purpose.
http://www.vbforums.com/showpost.php...5&postcount=13
Re: [RESOLVED]Threading and messageboxes
Hello!
I'm just wondering by using a MessageBox,
if the thread is already done how can you be able to close it?
Thanks!
Re: [RESOLVED]Threading and messageboxes
Quote:
Originally Posted by none123
Hello!
I'm just wondering by using a MessageBox,
if the thread is already done how can you be able to close it?
Thanks!
A MessageBox is intended to be dismissed by the user, so whether or not the thread has terminated would not be an issue in this case. When invoking a method using a delegate you call Invoke if you want the call to be synchronous, i.e. wait until the method passed to the delegate completes before continuing, or BeginInvoke if you want the call to be asynchronous, i.e. continue without waiting. I suggest that you do some reading on the subject.