We help IT Professionals succeed at work.

Possible to do *anything* with messagebox?

riceman0
riceman0 used Ask the Experts™
on

Hello, in my VB.NET program I continually monitor a serial port and display a status of a connected device, including "disconnected" if we haven't heard anything for a few seconds.

Well, whenever I throw up a modal msgbox I disconnect.  I guess this makes sense because the modal dialog box blocks my form.  However... is there any way to use modal messageboxes in an appllication and continue to handle certain events?  (such as my serial input)

I realize I can create my own forms and display them non-modally, but that is a pain and what I'm trying to avoid.  Besides, I *like* the modal behavior, it's appropriate -- I need the user to make a decision before he/she proceeds.  I just don't want to "disconnect" in the meantime.

Thanks for any thoughts.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
>>I need the user to make a decision before he/she proceeds.

I don't understand what choice the user has - you are posting some information, namely there isn't a connection.
Maybe you need the MessageBox modal behaviour for some things but a custom, non-modal, information box for other messages.

Commented:
call the modal ina different thread

Author

Commented:
"I don't understand what choice the user has"

For example, Yes/No/Cancel, Ok/Cancel... there are all sorts of options with the MessageBox and the return code is the user choice.

"call the modal ina different thread"

Mind if I ask if you have tried this?  I can think of several different ways to do this and it will end up a research project for sure.  I'd like to hear from someone who has attempted this before I undertake it.



AndyAinscowFreelance programmer / Consultant

Commented:
>>For example, Yes/No/Cancel, Ok/Cancel... there are all sorts of options with the MessageBox and the return code is the user choice.


Yes I understand that perfectly.

"Serial port is disconnected"  Yes/No/Cancel
??? What use is a choice for that?  It is a message saying the serial port is disconnected.

Author

Commented:

The serial port interaction has nothing to do with the messagebox, aside from the fact that they are two things I want to happen at the same time.  The messagebox is shown when the user, for example, opens a data file or confirms some data to be sent to the device.

My VB.NET program is talking to a hardware device (periodic status updates, on a timer).  At the same time it receives status updates from the device and updates a "device status" in the application status bar (which goes to "disconnected" if no messages).

When I show a modal messagebox, all of this stops: both my timer (output -- not sure if its the timer that gets blocked or the serial transmit though) and my serial port events (input), so both the device and my .NET program indicate DISCONNECTED.  I would like to allow serial port activity to continue w/o losing the convenience of modal dialog boxes; they really are useful.  I also want to do so elegantly, it is very possible to make things multithreaded the *wrong* way.

By the way  I am using a System.IO.Ports.SerialPort
AndyAinscowFreelance programmer / Consultant

Commented:
I think I misunderstood your problem.
If I understand it correctly then you need a custom form that is run modeless and you disable your main form to simulate a modal effect whilst the modeless form is visible.
You could multithread with a normal, modal, MessageBox but that might make some interaction more complex
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Do you have any kind of main processing loop dealing with your serialport?

Please show us some code that deals with sending messages out and also receiving messages.
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:

Well, like I said in the post the reason I'm posting is to see if there was a way to avoid creating custom forms, because modal messageboxes are very convenient, including the modal behavior.  I was hoping there was some way to keep the messagebox from blocking *certain portions* of your application.  I'll leave this open for a bit to see if there is a way, before I fall backto custom modeless forms.

Author

Commented:
Will look at that codecruiser.

Author

Commented:

Will try to tease out some code idlemind.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I'm guessing threading it as CodeCruiser suggests is probably the best solution.

...but it might be possible to "fix" your current code with some tweaks to make it work with the MessageBox.

*Can't really say for sure without seeing more code though*

=)
AndyAinscowFreelance programmer / Consultant

Commented:
As I said earlier you can fake a modal dialog by displaying a form modeless and disabling the original form.  That means it won't respond to user interaction such as mouse clicks but it does continue processing messages internally - so your current code for the port should keep working away as it currently does.