Overview
Features
Download
Documentation
Community
Add-Ons & Services

Code after waitForTerminationRequest not being run

Please post support and help requests here.

Code after waitForTerminationRequest not being run

Postby DougE » 10 Dec 2013, 16:16

I am using a ServerApplication with a TCPServer. I am developing with VS 2010 and I am running on a Win 7 machine. In my main I have a waitForTerminationRequest and then shutdown code is called after it returns. When I use CTRL-C to terminate everything works fine. When I close the window by clicking on the X the program terminates but does NOT run any of the code after the waitForTerminationRequest.

Anyone know what's happening or have ideas on how to determine the problem?

Thanks,
Doug Miller
DougE
 
Posts: 9
Joined: 17 Feb 2009, 17:43

Re: Code after waitForTerminationRequest not being run

Postby DougE » 10 Dec 2013, 23:47

After digging around some more I came across this discussion: http://stackoverflow.com/questions/3640 ... tine-issue

One of the posters said that on Win 7 a CTRL_CLOSE will terminate the process once the control handler has returned but will wait no longer than ~10 seconds for it to do so.

In my testing, my shutdown code that follows the waitForTerminationRequest runs on XP whether I close the window or do a CTRL+C. On Win 7 as noted, the CTRL+C allows it to run while the close exits as soon as the handler does.

The current code in ServerApplication for v1.4.5 is:

Code: Select all
BOOL ServerApplication::ConsoleCtrlHandler(DWORD ctrlType)
{
   switch (ctrlType)
   {
   case CTRL_C_EVENT:
   case CTRL_BREAK_EVENT:
   case CTRL_CLOSE_EVENT:
      terminate();
      return _terminated.tryWait(10000) ? TRUE : FALSE;
   default:
      return FALSE;
   }
}


I can only assume that maybe the tryWait call was an attempt at dealing with this situation but it doesn't seem to work. So for fun I changed it to the following which "works" in the sense that my shutdown code which takes about 2 seconds to run is now running on Win 7.

Code: Select all
BOOL ServerApplication::ConsoleCtrlHandler(DWORD ctrlType)
{
   switch (ctrlType)
   {
   case CTRL_C_EVENT:
   case CTRL_BREAK_EVENT:
      terminate();
      return _terminated.tryWait(10000) ? TRUE : FALSE;
   case CTRL_CLOSE_EVENT:
      terminate();
      // If I understand correctly, Windows stops the process as soon as
      // this method returns.  It seems that putting the Sleep here gives
      // the main thread sometime to run before getting squashed.
      Sleep(10000);
      // In this scenario I don't really know if this does anything but
      // I'm leaving it because I don't think it hurts.  Maybe this code
      // was supposed to do what the sleep seems to accomplish.
      return _terminated.tryWait(10000) ? TRUE : FALSE;
   default:
      return FALSE;
   }
}


Doug Miller
DougE
 
Posts: 9
Joined: 17 Feb 2009, 17:43


Return to Support

Who is online

Users browsing this forum: Schmendrick and 3 guests