How to immediately terminate crashed application?
Asked Answered
U

3

12

I have an application that sometimes causes exceptions. And I need to restart it if it crashed. But the problem is, that I have Windows 7 here and when application crashes, Windows shows me nice dialog box with a suggestion to close the application. But the application itself is still running until I click "Close". How to get rid of this Window and make application terminate immediately without any dialog boxes?

Unseen answered 24/12, 2010 at 23:24 Comment(2)
Another useful solution is the registry setting HKEY_CURRENT_USER\Control Panel\Desktop make a REG_SZ entry named AutoEndTasks and set it to 1Revoke
Then the question becomes: How to immediately terminate the specific (Process Id) crashed application?Aflutter
S
14

Ideally you would catch all exceptions in the most outer scope of your program. However, sometimes you don't have the luxury of making such changes and the crash dialogs prevent you from recovering from a crash. In these cases you can consider disabling Windows Error Reporting, either entirely or for a specific program.

On Windows 7: Start Orb -> Control Panel -> Action Center -> Maintenance -> Check for solutions to problem reports -> Settings

Update: to completely disable the error reporting UI change the DontShowUI registry setting:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\DontShowUI

Suetonius answered 25/12, 2010 at 2:3 Comment(4)
It's not my application, that crashes. And this reporting is disabled. But Windows still shows informing dialog, that prevents crashed executable from termination until you press Ok there.Unseen
Updated the answer to include a registry setting.Suetonius
I also have an application which is not mine that crashes some times. I'd like to ensure that when it crashes, that it automatically terminates immediately without requiring the user the press a button in a notification window.Stollings
To disable error reporting set DontShowUI to DWORD 0Revoke
B
1

You can use the concept of the Dr Watson for detecting the crash and restarting the application. Use the following:

Set a Timer like:

SetTimer( eIsDwwin, 30000, NULL);

This will check the Dr Watson process after every 30 seconds in the system process using the following:

void CMainFrame::OnTimer(UINT nIDEvent) {

    case eIsDwwin:
        HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        DWORD dwsma = GetLastError();
        DWORD dwExitCode = 0;
        PROCESSENTRY32  procEntry={0};
        procEntry.dwSize = sizeof( PROCESSENTRY32 );
        Process32First(hndl,&procEntry);
        do {
            if(((!strcmpi(procEntry.szExeFile,"dwwin.exe"))|| 
                (!strcmpi(procEntry.szExeFile,"drwatson.exe"))||
                (!strcmpi(procEntry.szExeFile,"drwtsn32.exe"))||
                (!strcmpi(procEntry.szExeFile,"WerFault.exe"))) &&
                (procEntry.th32ParentProcessID == GetCurrentProcessId())) {

                WSACleanup();
                PostMessage( WM_CLOSE);
                Sleep(500);

        if(0==strcmpi(procEntry.szExeFile,"dwwin.exe")) {
            system("TASKKILL /IM dwwin.exe /F");
        } else if(0==strcmpi(procEntry.szExeFile,"drwatson.exe")) {
            system("TASKKILL /IM drwatson.exe /F");
        } else if(0==strcmpi(procEntry.szExeFile,"drwtsn32.exe")) {
            system("TASKKILL /IM drwtsn32.exe /F");
            } else if(0==strcmpi(procEntry.szExeFile,"WerFault.exe")) {
            system("TASKKILL /IM WerFault.exe /F");
        } else {
                    system("TASKKILL /IM WerFault.exe /F");
        }
                break;
        }
    } while(Process32Next(hndl,&procEntry));

        CloseHandle(hndl);
    break;
    }
}

This will close the application which has crashed, gracefully.

Anothe way would be to use the Signal API in Windows for handling the exceptions and crashes.

Bab answered 6/6, 2012 at 7:21 Comment(0)
D
0

Not knowing the language of your application makes it a little tough, but you can typically handle that by running your application within the equivalent of a try / catch block (as in C#, C++, or Python). In pseudo-Csharp-ish-code:

bool done = false;
main()
{
    while( !done )
    {
        AppClass aclass = new AppClass();
        try
        {
            aclass->Run();
        }
        catch( Exception x )
        {
             LogErrorSomehow(x);
             delete aclass;
             continue;
        }
    }
}

Essentially, the best way to get around an "unhandled exception" error is to handle the exception by catching it and taking appropriate action (even if that action is doing nothing).

In this example, if someone wanted to close your app you'd set the "done" value to true and return from Run().

Dreadful answered 24/12, 2010 at 23:45 Comment(1)
Crashing application is not mine.Unseen

© 2022 - 2024 — McMap. All rights reserved.