Let me give you the background.
We have an Application(medium sized) that is using MessageBox.Show (....) at various places (in hundreds).
These message boxes are part of workflow and being used for informing,warning or taking input from an user. Application is supposed to automatically log off after certain time if there is no activity. We have a requirement that while logging out the application, just to clean the session data , to clear views and to hide itself so that in next launch, it won't have to execute the startup process which is costly in terms of time.
Everything is working fine but in a scenario when there is some message box on the screen and user left the machine without responding to message box and then due to no activity to make the application to log out. Problem is Message box won't disappear.
How I can close the opened messagebox, if any, while hiding the application?
Create your own control for this and implement behavior you like to have there. As an option there may be a timer to close this MessageBox.
Refer to DmitryG post in "Close a MessageBox after several seconds"
Auto-Close MessageBox after timeout reach
and Call it via
Here is a piece of code based on UIAutomation (a cool but still not very used API) that attempts to close all modal windows (including the one opened with MessageBox) of the current process:
For example, if you have a WinForms application that pops up a MessageBox when you press some button1, you will still be able to close the app using Windows "Close Window" menu (right click in the task bar):
You could use CloseModalWindows somewhere else in your code of course, this is just a sample.
I used .net 2 and two approaches with the same trick.
Open the MessageBox from stub-Form with
MessageBox.Show(this,"message")
When the form is not visible or doesn't has really UI.
Keep the form handler and close it with:
or
holding the form as class parameter and using
FormX.Close()
.Since the Form is the owner of the MessageBox, Closing it will close the MessageBox.
First a Question: If messages boxes are used as part of workflow, won't programatically closing message box cause the flow to change/continue?
I think you have three options
Create your own version of the messagebox class that opens a dialog window that looks like a messagebox with added functionality so it closed automatically after a period of time.
Implement something like this in c# to close message boxes programtically. http://www.codeproject.com/KB/dialog/AutoCloseMessageBox.aspx
Get rid of the message boxes from interupting the workflow. This is probably the best solution as from the sound of it closing a message box programatically will cause workflow to continue/change, and perhaps even cause another messagebox to show which may not be desirable. But obviously fixing the root problem might be best, but isn't always the easiest.
1 and 2 would need to be done from a separate thread, so you will need to think about the implications of that as showing the messagebox will be blocking.
This link on MSDN forums shows how to close a message box by using
FindWindow
and sending aWM_CLOSE
message. Although the question was asked for .NET/WindowsCE, it might solve your problem, its worth a look