TimeBox MessageBox OR Closing a MessageBox from another thread

If my application crashes, I use ExceptionFilter to catch the crash, perform some final actions, and then show the user a message box saying that the application crashed.

Since the application has already crashed, I cannot (or I dared) do this, because if I do too much, the executable can access the damaged memory and crash again. Some of the things that I cannot do right now (or I dare not do) are to close network connections, Oracle database sessions, ...

The problem is that if the application crashes and the user goes out for lunch while the MessageBox is open, other users may be blocked due to an open database session. Therefore I want:

  • Or a MessageBox with a timeout. The problem is that you cannot do this with the standard MessageBox Win32 API function, and I do not want to make a specific dialog for it (because I want to minimize the logic that is executed after the failure).
  • Or the ability to close a MessageBox from another thread (another thread may provide timeout logic).

I missed something in the Win32 API and is it possible to have a MessageBox with a timeout?

Or how to properly close an open MessageBox from another thread (how to get a MessageBox handle, how to close it, ...)?

+4
source share
8 answers

I noticed that if the main thread just exits the application while the :: MessageBox is still open in another thread, then the MessageBox is accepted by the CSRSS process. This solves my problem as it only requires a timeout in the event in the main thread (WaitForSingleObject with a timeout).

However, this raised another question: https://stackoverflow.com/questions/3091915/explanation-why-messagebox-of-exited-application-is-adopted-by-winsrv .

+2
source

While I agree that the emergence of a new process to display the fire and swell dialog is probably best, FWIW actually has a timeout function that is exported from user32 to XP and higher; MessageBoxTimeout (used by things like WShell.Popup() )

+5
source

You must ask yourself why you want the mailbox to be created first. When everything is normal, that the message box is not visible, when no one is sitting in front of the computer, why is it not normal that the user does not see the message when his program disappears?

If you really want this, I think the easiest solution is to have a new process display the message. It can work as long as it wants, and does not interfere with your emergency program.

+3
source

This does not justify the flow.

A better solution would be to use a modal dialog box that registers a timer for automatic closure.

+1
source

How easy is it to register an event in a local file (and write memory dumps or any other information that may be needed for subsequent debugging)?

You can close your application, close network connections and complete your service records.

As soon as the application starts again, you can inform your user (based on the local file information) that the application crashed during the last run.

+1
source

Quick copy / paste:

 int DU_MessageBoxTimeout(HWND hWnd, const WCHAR* sText, const WCHAR* sCaption, UINT uType, DWORD dwMilliseconds) { // Displays a message box, and dismisses it after the specified timeout. typedef int(__stdcall *MSGBOXWAPI)(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); int iResult; HMODULE hUser32 = LoadLibraryA("user32.dll"); if (hUser32) { auto MessageBoxTimeoutW = (MSGBOXWAPI)GetProcAddress(hUser32, "MessageBoxTimeoutW"); iResult = MessageBoxTimeoutW(hWnd, sText, sCaption, uType, 0, dwMilliseconds); FreeLibrary(hUser32); } else iResult = MessageBox(hWnd, sText, sCaption, uType); // oups, fallback to the standard function! return iResult; } 
+1
source

I would run your source code from a shell application that creates CreateProcess, and then MsgWaitForMultipleObjects in the process descriptor (most sample process start code uses WaitForSingleObject, but you need to protect yourself from messaging deadlock scripts). After that, the viewing process can detect a failure of the generated process, open its own timeout and exit the user response or timeout.

I think the cleanest solution that prevents your code from executing your unstable program.

0
source

Win32 MessageBox is a message dialog box with a message message. Therefore, you can rely on standard Win32 timer messages (WM_TIMER). Send it to your window, and when you receive it, release the MessageBox by sending the message WM_COMMAND/BN_CLICKED to the ID_OK button.

In the message window, since this is a dialog, there will be a class "# 32770". Since this is the only dialog you open, it is easy to find among your child windows.

0
source

Source: https://habr.com/ru/post/1313486/


All Articles