Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:48:44+00:00 2026-05-25T14:48:44+00:00

Here is a simple application that handled CTRL + C signal both on linux

  • 0

Here is a simple application that handled CTRL+C signal both on linux and windows:

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QThread>

void SigIntHandler()
{
    qDebug()<<"SigInt ThreadID: "<<QThread::currentThreadId();
    qApp->quit();
}

#ifdef __linux__
#include <signal.h>

    void unix_handler(int s)
    {
        //svakako je SIGINT, ali da ne javlja warning da se s ne koristi
        if (s==SIGINT)
            SigIntHandler();
    }

#else
#include <windows.h>

    BOOL WINAPI WinHandler(DWORD CEvent)
    {
        switch(CEvent)
        {
        case CTRL_C_EVENT:
            SigIntHandler();
            break;
        }
        return TRUE;
    }

#endif

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    //kod za hvatanje CTRL+C - unix i windows
    #ifdef __linux__
        signal(SIGINT, &unix_handler);
    #else
        SetConsoleCtrlHandler((PHANDLER_ROUTINE)WinHandler, TRUE);
    #endif

    qDebug()<<"Main ThreadID: "<<QThread::currentThreadId();
    return a.exec();
}

After compiling and running it on linux (Debian Squeeze), I get following output:

/Test-build-desktop$ ./Test 
Main ThreadID:  140105475446560 
^CSigInt ThreadID:  140105475446560 

/Test-build-desktop$ ./Test 
Main ThreadID:  140369579480864 
^CSigInt ThreadID:  140369579480864 

/Test-build-desktop$ ./Test 
Main ThreadID:  140571925509920 
^CSigInt ThreadID:  140571925509920 

And that’s what I’ve expected (SigIntHandler method runs on main thread). But when I compile and execute same code on Windows 7, I get this:

d:\Test-build-desktop\debug>Test.exe
Main ThreadID:  0x5a8
SigInt ThreadID:  0x768

d:\Test-build-desktop\debug>Test.exe
Main ThreadID:  0x588
SigInt ThreadID:  0x1434

d:\Test-build-desktop\debug>Test.exe
Main ThreadID:  0x1170
SigInt ThreadID:  0xc38

As you can see, here SigIntHandler method is executed in different thread then main … And that creates me many problems. So my question is – is it possible to force SigIntHandler to run in main thread on windows ? Am I maybe catching siging wrong ?

Thanks !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:48:45+00:00Added an answer on May 25, 2026 at 2:48 pm

    From MSDN topic HandlerRoutine:

    A HandlerRoutine function is an application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.

    So, the answer is: this is impossible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simple, 45 line console application that reproduces the bug on my Win
Here's the situation: I'm developing a simple application with the following structure: FormMain (startup
Anybody tried here using Getting Real(37Signals) approach to develop windows application? (C#/.NET). Or simply
My application is simple, I have 2 pages: RSSProducer.aspx: A page that generates RSS
This is beyond both making sense and my control. That being said here is
I'm trying to write a simple winforms application that executes a SQL SELECT statement
I have a very simple command line Java application that I wish to port
I'm trying to build a simple Android application using NDK. Here are the contents
Ok I am near wits end here. I've got a simple MVC3 application with
I'm probably missing something simple here, but I can't find the answer elsewhere. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.