Results 1 to 7 of 7

Thread: [RESOLVED]Threading and messageboxes

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    [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
    Last edited by mpdeglau; Sep 29th, 2005 at 10:42 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Delegate Sub ShowMessageDelegate(ByVal message As String)
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim t As New Thread(AddressOf ThreadStart)
    5.  
    6.         t.Start()
    7.     End Sub
    8.  
    9.     Private Sub ThreadStart()
    10.         Me.Invoke(New ShowMessageDelegate(AddressOf ShowMessage), New Object() {"Hello"})
    11.     End Sub
    12.  
    13.     Private Sub ShowMessage(ByVal message As String)
    14.         MessageBox.Show(message)
    15.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [RESOLVED]Threading and messageboxes

    Thanks jm. That worked

  4. #4
    Addicted Member
    Join Date
    Jul 2005
    Posts
    153

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    New Member
    Join Date
    Sep 2005
    Posts
    8

    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!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width