I need to ensure that an app is closed just before Midnight.
The code below generally works, but every week or so it stays running.
What might be casuing Environment.Exit(0) NOT to work?
As you can see from FmLog, it does get to write the "Halting" message:
[GetRecordsCallBack] Start: 10-07-2019 23:53:44
[GetRecordsCallBack] Start: 10-07-2019 23:54:44
[GetRecordsCallBack] Start: 10-07-2019 23:55:44
[GetRecordsCallBack] Start: 10-07-2019 23:56:44
[GetRecordsCallBack] Halting: 10-07-2019 23:56:44
public static void GetRecordsCallBack(object source)
{
Console.WriteLine("[GetRecordsCallBack] Start: " + DateTime.UtcNow.ToString("HH:mm:ss"));
File.AppendAllText(@"\\bb\FmLog", "[GetRecordsCallBack] Start: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");
GetRecords();
if (DateTime.UtcNow.TimeOfDay > new TimeSpan(23, 56, 00))
{
ThreadPool.QueueUserWorkItem(state =>
{
Thread.Sleep(2000);
Environment.Exit(0);
//System.Diagnostics.Process.GetCurrentProcess().Kill();
});
GetRecordsTimer.Change(Timeout.Infinite, Timeout.Infinite);
File.AppendAllText(@"\\bb\FmLog", "[GetRecordsCallBack] Halting: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");
return;
}
GetRecordsTimer = new System.Threading.Timer(new TimerCallback(GetRecordsCallBack), null, 60000, 0);
}