Multithread and MessageBox

M

Mystery Man

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??
 
W

William Stacey

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.2003JUL.1033/cpguide/html/cpcondevelopingm
ultithreadedwindowsformscontrol.htm
 
M

Mystery Man

William Stacey said:
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.2003JUL.1033/cpguide/html/cpcondevelopingm
ultithreadedwindowsformscontrol.htm

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?
 
W

William Stacey

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.
 
M

Mystery Man

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??).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top