简体   繁体   中英

wxWidgets C++ console application: how to capture INT/TERM signals?

I have a wxWidgets console application, and I need to handle SIGINT / SIGTERM signals to shutdown program properly.

I've tried "classic" signals handling, but it does not works:

#include <signal.h>

bool MainApp::OnInit()
{   // terminate gracefully:
    // term threads, wait them for terminate, etc.
}

static void OnSignal(int sig)
{   // captured signal:
    cout<<"Terminating...\r\n";
    wxGetApp().OnExit();
}

bool MainApp::OnInit()
{
    // init app here, create working threads, etc.
    // set own handler:
    cout<<"Signal set handler result: "<<signal(SIGINT,OnSignal);// returns 0
    return true;// app inited successfully
}

When I send SIGINT or SIGTERM signals (using integrated CodeLite's terminal for debugging), nothing happens.

Looks like wxWidgets still does not have signal processing - at least, I've found nothing in documentation.

The question is: how to capture INT/TERM signals? (At least in Linux, but cross-platform solution is of course better) .

You can't use cout nor exit the application from a signal handler, so while I don't know why it isn't called at all for you, things would only be worse, not better, if it was.

wxWidgets has private (ie undocumented, liable to change) wxApp::SetSignalHandler() in Unix ports which implements proper signal handling, with the handler itself just waking up the event loop and telling it to wait for the signals. If you do anything other than just setting a flag in your signal handler, this is what you should do as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM