Multithread and MessageBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mystery Man

    #1

    Multithread and MessageBox

    We have developed a CSharp program that has a secondary thread that is
    used to perform background operations. This secondary thread may
    display some MessageBoxes or Forms.

    However, if the secondary thread is aborted, then the MessageBox
    control does not get deleted. The forms do. I am unsure why this
    should be the case because MessageBox is also derived from
    System.Windows. Forms.

    Is there any way I can get the MessageBoxes to be deleted when I abort
    the secondary thread??
  • William Stacey

    #2
    Re: Multithread and MessageBox

    I would change this around a bit. Your second thread should not have any UI
    elements. It will just be a free thread that does its thing and calls
    Invoke on a Windows form/control to allow *it to handle the message boxes or
    put up other forms, etc. Your working thread can also check for a canceled
    flag property in the loop and exit when true.
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 JUL.1033/cpguide/html/cpcondeveloping m
    ultithreadedwin dowsformscontro l.htm

    --
    William Stacey, MVP

    "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
    news:87c81238.0 312162202.587f4 e6b@posting.goo gle.com...[color=blue]
    > We have developed a CSharp program that has a secondary thread that is
    > used to perform background operations. This secondary thread may
    > display some MessageBoxes or Forms.
    >
    > However, if the secondary thread is aborted, then the MessageBox
    > control does not get deleted. The forms do. I am unsure why this
    > should be the case because MessageBox is also derived from
    > System.Windows. Forms.
    >
    > Is there any way I can get the MessageBoxes to be deleted when I abort
    > the secondary thread??[/color]


    Comment

    • Mystery Man

      #3
      Re: Multithread and MessageBox

      "William Stacey" <staceywREMOVE@ mvps.org> wrote in message news:<OggQvsGxD HA.3408@tk2msft ngp13.phx.gbl>. ..[color=blue]
      > I would change this around a bit. Your second thread should not have any UI
      > elements. It will just be a free thread that does its thing and calls
      > Invoke on a Windows form/control to allow *it to handle the message boxes or
      > put up other forms, etc. Your working thread can also check for a canceled
      > flag property in the loop and exit when true.
      > ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 JUL.1033/cpguide/html/cpcondeveloping m
      > ultithreadedwin dowsformscontro l.htm
      >
      > --
      > William Stacey, MVP
      >
      > "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
      > news:87c81238.0 312162202.587f4 e6b@posting.goo gle.com...[color=green]
      > > We have developed a CSharp program that has a secondary thread that is
      > > used to perform background operations. This secondary thread may
      > > display some MessageBoxes or Forms.
      > >
      > > However, if the secondary thread is aborted, then the MessageBox
      > > control does not get deleted. The forms do. I am unsure why this
      > > should be the case because MessageBox is also derived from
      > > System.Windows. Forms.
      > >
      > > Is there any way I can get the MessageBoxes to be deleted when I abort
      > > the secondary thread??[/color][/color]

      Thanks William, Changing it around as you suggest is not an easy
      option for us. I have also read this article on multithreaded stuff.

      Basically, the threading works like a charm for us and I really dont
      want to do an overhaul as you suggest just to get a messageBox to
      disappear.

      Do you (or anyone else) have any other ideas?

      Comment

      • William Stacey

        #4
        Re: Multithread and MessageBox

        Maybe I did not explain that fully. I am not suggesting big changes here.
        Use your threads as is, just take the message box out of the worker thread
        and put in a form method which gets called with the Invoke method. Pretty
        minor unless I missed something. Handle all UI elements in your form. This
        can have other benefits. Your message box can be tied to the form as the
        owner, you can get a clean shutdown/canel mechanizum for your free thread
        without it having spurious UI elements which can cause issues with not clean
        shutdowns, and your thread will just do the right thing when the form exits
        and can't just hang around (unless it is stuck on something.) Then again,
        you know your code best, but that is one approach.

        --
        William Stacey, MVP

        "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
        news:87c81238.0 312171521.7d425 b39@posting.goo gle.com...[color=blue]
        > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message[/color]
        news:<OggQvsGxD HA.3408@tk2msft ngp13.phx.gbl>. ..[color=blue][color=green]
        > > I would change this around a bit. Your second thread should not have[/color][/color]
        any UI[color=blue][color=green]
        > > elements. It will just be a free thread that does its thing and calls
        > > Invoke on a Windows form/control to allow *it to handle the message[/color][/color]
        boxes or[color=blue][color=green]
        > > put up other forms, etc. Your working thread can also check for a[/color][/color]
        canceled[color=blue][color=green]
        > > flag property in the loop and exit when true.
        > >[/color][/color]
        ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 JUL.1033/cpguide/html/cpcondeveloping m[color=blue][color=green]
        > > ultithreadedwin dowsformscontro l.htm
        > >
        > > --
        > > William Stacey, MVP
        > >
        > > "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
        > > news:87c81238.0 312162202.587f4 e6b@posting.goo gle.com...[color=darkred]
        > > > We have developed a CSharp program that has a secondary thread that is
        > > > used to perform background operations. This secondary thread may
        > > > display some MessageBoxes or Forms.
        > > >
        > > > However, if the secondary thread is aborted, then the MessageBox
        > > > control does not get deleted. The forms do. I am unsure why this
        > > > should be the case because MessageBox is also derived from
        > > > System.Windows. Forms.
        > > >
        > > > Is there any way I can get the MessageBoxes to be deleted when I abort
        > > > the secondary thread??[/color][/color]
        >
        > Thanks William, Changing it around as you suggest is not an easy
        > option for us. I have also read this article on multithreaded stuff.
        >
        > Basically, the threading works like a charm for us and I really dont
        > want to do an overhaul as you suggest just to get a messageBox to
        > disappear.
        >
        > Do you (or anyone else) have any other ideas?[/color]


        Comment

        • Mystery Man

          #5
          Re: Multithread and MessageBox

          Thanks again William. this would mean a bit of work as we run the same
          engine from the main thread and from a secondary thread. In addition,
          we have numerous message boxes (and forms) called from this engine.

          Bear in mind that the reason why this secondary thread is initiated is
          so that the user can suspend what they were doing (in the second
          thread) and jump back to the main thread application. If we put the
          message boxes into the main thread. then they would have to respond to
          the message box before they can get access to other forms in the main
          form (I think). This is not what we want. We want the users to get
          access to the rest of the application ASAP, ie hit a suspend button
          (on the second thread) and go straight back to the main thread).

          Again, all I awnt to do is close down the message boxes that get shown
          in the second thread when this gets aborted. I presume I could do a
          PostMessage (or something like that??).


          "William Stacey" <staceywREMOVE@ mvps.org> wrote in message news:<#CrgCWQxD HA.3208@tk2msft ngp13.phx.gbl>. ..[color=blue]
          > Maybe I did not explain that fully. I am not suggesting big changes here.
          > Use your threads as is, just take the message box out of the worker thread
          > and put in a form method which gets called with the Invoke method. Pretty
          > minor unless I missed something. Handle all UI elements in your form. This
          > can have other benefits. Your message box can be tied to the form as the
          > owner, you can get a clean shutdown/canel mechanizum for your free thread
          > without it having spurious UI elements which can cause issues with not clean
          > shutdowns, and your thread will just do the right thing when the form exits
          > and can't just hang around (unless it is stuck on something.) Then again,
          > you know your code best, but that is one approach.
          >
          > --
          > William Stacey, MVP
          >
          > "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
          > news:87c81238.0 312171521.7d425 b39@posting.goo gle.com...[color=green]
          > > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message[/color]
          > news:<OggQvsGxD HA.3408@tk2msft ngp13.phx.gbl>. ..[color=green][color=darkred]
          > > > I would change this around a bit. Your second thread should not have[/color][/color]
          > any UI[color=green][color=darkred]
          > > > elements. It will just be a free thread that does its thing and calls
          > > > Invoke on a Windows form/control to allow *it to handle the message[/color][/color]
          > boxes or[color=green][color=darkred]
          > > > put up other forms, etc. Your working thread can also check for a[/color][/color]
          > canceled[color=green][color=darkred]
          > > > flag property in the loop and exit when true.
          > > >[/color][/color]
          > ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 JUL.1033/cpguide/html/cpcondeveloping m[color=green][color=darkred]
          > > > ultithreadedwin dowsformscontro l.htm
          > > >
          > > > --
          > > > William Stacey, MVP
          > > >
          > > > "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
          > > > news:87c81238.0 312162202.587f4 e6b@posting.goo gle.com...
          > > > > We have developed a CSharp program that has a secondary thread that is
          > > > > used to perform background operations. This secondary thread may
          > > > > display some MessageBoxes or Forms.
          > > > >
          > > > > However, if the secondary thread is aborted, then the MessageBox
          > > > > control does not get deleted. The forms do. I am unsure why this
          > > > > should be the case because MessageBox is also derived from
          > > > > System.Windows. Forms.
          > > > >
          > > > > Is there any way I can get the MessageBoxes to be deleted when I abort
          > > > > the secondary thread??[/color]
          > >
          > > Thanks William, Changing it around as you suggest is not an easy
          > > option for us. I have also read this article on multithreaded stuff.
          > >
          > > Basically, the threading works like a charm for us and I really dont
          > > want to do an overhaul as you suggest just to get a messageBox to
          > > disappear.
          > >
          > > Do you (or anyone else) have any other ideas?[/color][/color]

          Comment

          Working...