title: Restart service while it is busy
body: <p>I have written windows service application using Delphi XE2.
  Recently I noticed that service failed to start after Windows update. Logs told me that service was busy doing sleep.</p>
  <p>I have</p>
  <pre><code>procedure TnSoftTestService.TestCheckEverything;
  var
    i: Integer;
  begin
    for i := 0 to 24 do
    begin
      Sleep(1000);
      LogToFile(IntToStr(ord(nSoftTestService.Status)), TLogMsgType.ltDebug);
    end;
  end;
  
  procedure TnSoftTestService.ServiceCreate(Sender: TObject);
  begin
    LogToFile('Servisas kuriamas', TLogMsgType.ltDebug);
  end;
  
  procedure TnSoftTestService.ServiceStart(Sender: TService;
    var Started: Boolean);
  begin
    CoInitializeEx(nil, COINIT_MULTITHREADED);
    LogToFile('statring', TLogMsgType.ltWarning);
    Application.CreateForm(TdmMainDataModule, dmMainDataModule);
    dmMainDataModule.svsRysiams.active := True;
  end;
  
  procedure TnSoftTestService.ServiceStop(Sender: TService; var Stopped: Boolean);
  begin
    LogToFile('Servisas stabdomas', TLogMsgType.ltWarning);
  end;
  </code></pre>
  <p>dmMainDataModule contains TServerSocket that i connect to and execute <code>TnSoftTestService.TestCheckEverything;</code>. while it is in a loop, i restart service. I get &quot;Service is stopping&quot; and then &quot;Service is starting&quot; in Windows services, but i get no <code>ServiceStart</code> logs and after sleep loop i get <code>ServiceStop</code> log and service is completely stopped. I do get them if I do not execute <code>TestCheckEverything</code>. Where should I put my long sleep and how do i detect that service is being restarted?</p>