Click to See Complete Forum and Search --> : Start / Stop Service. What is the better approach?


Ionline
January 10th, 2004, 04:45 AM
Hi,

I am planning to use StartService or ControlService api for starting or stopping a service using either . To break out of start/stop pending servive, I have observed that we can use either two ways as follows:

// Wait for the service to stop
while ( ss.dwCurrentState != SERVICE_STOPPED ) {

Sleep( ss.dwWaitHint );
if ( !QueryServiceStatus( hDepService, &ss ) )
return GetLastError();

if ( ss.dwCurrentState == SERVICE_STOPPED )
break;

if ( GetTickCount() - dwStartTime > dwTimeout )
return ERROR_TIMEOUT;
}
where dwStartTime = GetTickCount() during start of program and
dwTimeout will be a signature for the function.

OR

DWORD dwChkP ;
while ( ss.dwCurrentState != SERVICE_STOPPED ) {
dwChkP = ssStatus.dwCheckPoint;
Sleep( ss.dwWaitHint );
if ( !QueryServiceStatus( hDepService, &ss ) )
return GetLastError();

if ( ss.dwCurrentState == SERVICE_STOPPED )
break;

if(ssStatus.dwCheckPoint < dwChkP)
break;
}
if (SERVICE_RUNNING != ssStatus.dwCurrentState)
return ERROR_TIMEOUT;
// Here I dont have to worry about dwTimeout

Which is the better approach from the above code snippets??

Andreas Masur
January 10th, 2004, 05:57 AM
Well...none of them...take a look at the following sample (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/starting_a_service.asp) which shows it for starting a service...the adaption for stopping should be obvious... :cool: