[ back to toc ]

OleMainWndThread Message...

Date: 2002/02/15 09:29

Q:
Hello Peter:

I'm developing with Visual C++ on Windows NT. I am trying to write a
service in which the main thread spawns off a listen thread, and in turn
spawns off a client thread to do the work. Using semaphores, I limit the
client threads available.

Through a lot of examples and tutorials, I think I got the multi-thread
design working and I wrapped it all up in as a WinNT service.

My problem now is that I can automatically start, manually start and
manually stop the service, but on shutdown, the service doesn't stop until
after I hit the End Task button of the OleMainWndThread message box that
pops up. Using message boxes to debug, I know I am calling my
StopService() code after WinNT has shutdown (i.e. right before the Restart
WinNT message box).

What on earth am I doing wrong? Here is my ControlHandler code:

void WINAPI MyControlHandler(DWORD dwControl){

DWORD rc;

switch(dwControl){
case SERVICE_CONTROL_PAUSE:
case SERVICE_CONTROL_CONTINUE:
case SERVICE_CONTROL_INTERROGATE:
gStatus.dwWaitHint = 0;
break;
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
gStatus.dwCurrentState = SERVICE_STOP_PENDING;
gStatus.dwWaitHint = 5000;
MessageBox(NULL, "Stop or shutdown", "Testing...", MB_OK);
break;
}

rc = SetService(ghCtrlStatus, gStatus);
if(!rc){
MessageBox(NULL, "Error in Control Handler", "Error", MB_ICONERROR);
DebugBreak();
}

if((dwControl == SERVICE_CONTROL_STOP || dwControl ==
SERVICE_CONTROL_SHUTDOWN)){
MessageBox(NULL, "Calling Stop Service code...", "Testing...", MB_OK);
StopService();
(void)SetEvent(ghevDoForever);
}

}

I am calling the StopService code, but only after everything has shutdown.
I wouldn't get the OleMainWndThread message box if it had called the
StopService function at the correct time in the first place. Is this a
race condition? Is the above code snippet correct and it could be
something else.

I am a junior programmer so I hope I am making sense. Thanks for any
suggestions in advance.

A:
I am not sure, but I guess that the threads started should call
TerminateThread to stop.

However when a service is to be stopped, it does not mean anything else
for the Service Control Manager (SCM) that the service reports that it did
stop. Actually the SCM sends the service a one byte code to get to that
very state. Only three of 256 possible values are used to RUN, PAUSE and
STOP. Principly it would be possible to replace the SCM to other service
manager that could send different codes to services.

Notice that if you ever stop IIS the process is still there. If you start
an ISAPI application that gets into an infinite loop, you will not be able
to stop the service.

Regards,
Peter

[ back to toc ]