Bassam72

Dear All,

I am writing a network application using the native win32 with microsoft visual C++ 2005 express edition on windows 2003 xp pro. The main file of the application keeps running within a loop, and I would like to get out of the loop for house keeping whenever I press "ctrl + c" or any combination that could be determined.

This is the main program of my application:


Re: Visual C++ Language How to get out of a loop by pressing a combination of keys

Viorel.

For performing final actions before your program exits (including via <Ctrl+C>), I think you can try the atexit function:

void houseKeeping()

{

// house keeping stuff

. . .
}

void main()

{

atexit(houseKeeping);

. . .
}

For intercepting <Ctrl+C> event you can also use signal(SIGINT, ...) or SetConsoleCtrlHandler functions.

I hope this helps.





Re: Visual C++ Language How to get out of a loop by pressing a combination of keys

Simple Samples

Console Signals Handler




Re: Visual C++ Language How to get out of a loop by pressing a combination of keys

Bassam72

Well, I have some point here that all my house keeping stuff cannot be transfered or kept on a separate routin or function. They have to be in the same main routin. However, I used the signal(SIFINT,...) and calls a function when CTRL+ C are pressed, and in turn that function will set a global variable that has to be checked inside the loop of the main program and as it has shown in the following code snippet.




Re: Visual C++ Language How to get out of a loop by pressing a combination of keys

Viorel.

Bassam72 wrote:

[...]

I wounder if I have done the right way or not. Please correct me if there is a better way.

[...]

I think your solution is correct.