I have a windows service that is consuming an API that every time it starts, it sends data to the API and when the service is disabled this information needs to be taken to the API. I'm using Thread, however it does not respect the time it was determined, so it does not give time for it to send information that the service was stopped for API.
How can I delay the service stop and allow time for the data to arrive in the API?
protected override void OnStart(string[] args)
{
_writeFile.Log("Foi iniciado o serviço");
_shutdownEvent = new ManualResetEvent(false);
_thread = new Thread(async () => await ControlSendHistorico());
_thread.Start();
base.OnStart(args);
}
protected override void OnStop()
{
if (_stopping) return;
try
{
_stopping = true;
_shutdownEvent.Set();
//_shutdownEvent.WaitOne(30000);
_thread.Join(30000);
_writeFile.Log("O serviço foi parado");
base.OnStop();
}
catch (Exception e)
{
_writeFile.Log("Falha ao parar o serviço " + e.Message);
}
finally
{
_stopping = false;
}
}