我需要确保某个应用程序在午夜之前关闭。
下面的代码通常可以运行,但是大约每星期都会运行一次。
是什么原因导致Environment.Exit(0)无法正常工作?
从FmLog中可以看到,确实可以写出“ Halting”消息:
[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);
}