+ service->status.dwCurrentState = SERVICE_PAUSED;\r
+ SetServiceStatus(service->status_handle, &service->status);\r
+\r
+ if (use_critical_section) {\r
+ imports.SleepConditionVariableCS(&service->throttle_condition, &service->throttle_section, ms);\r
+ LeaveCriticalSection(&service->throttle_section);\r
+ }\r
+ else {\r
+ if (service->throttle_timer) WaitForSingleObject(service->throttle_timer, INFINITE);\r
+ else Sleep(ms);\r
+ }\r
+}\r
+\r
+/*\r
+ When responding to a stop (or any other) request we need to set dwWaitHint to\r
+ the number of milliseconds we expect the operation to take, and optionally\r
+ increase dwCheckPoint. If dwWaitHint milliseconds elapses without the\r
+ operation completing or dwCheckPoint increasing, the system will consider the\r
+ service to be hung.\r
+\r
+ However the system will consider the service to be hung after 30000\r
+ milliseconds regardless of the value of dwWaitHint if dwCheckPoint has not\r
+ changed. Therefore if we want to wait longer than that we must periodically\r
+ increase dwCheckPoint.\r
+\r
+ Furthermore, it will consider the service to be hung after 60000 milliseconds\r
+ regardless of the value of dwCheckPoint unless dwWaitHint is increased every\r
+ time dwCheckPoint is also increased.\r
+\r
+ Our strategy then is to retrieve the initial dwWaitHint and wait for\r
+ NSSM_SERVICE_STATUS_DEADLINE milliseconds. If the process is still running\r
+ and we haven't finished waiting we increment dwCheckPoint and add whichever is\r
+ smaller of NSSM_SERVICE_STATUS_DEADLINE or the remaining timeout to\r
+ dwWaitHint.\r
+\r
+ Only doing both these things will prevent the system from killing the service.\r
+\r
+ Returns: 1 if the wait timed out.\r
+ 0 if the wait completed.\r
+ -1 on error.\r
+*/\r
+int await_shutdown(nssm_service_t *service, TCHAR *function_name, unsigned long timeout) {\r
+ unsigned long interval;\r
+ unsigned long waithint;\r
+ unsigned long ret;\r
+ unsigned long waited;\r
+ TCHAR interval_milliseconds[16];\r
+ TCHAR timeout_milliseconds[16];\r
+ TCHAR waited_milliseconds[16];\r
+ TCHAR *function = function_name;\r
+\r
+ /* Add brackets to function name. */\r
+ size_t funclen = _tcslen(function_name) + 3;\r
+ TCHAR *func = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, funclen * sizeof(TCHAR));\r
+ if (func) {\r
+ if (_sntprintf_s(func, funclen, _TRUNCATE, _T("%s()"), function_name) > -1) function = func;\r
+ }\r