我正在开发一个数据采集应用程序,希望确保程序能够优雅地退出。也就是说,程序需要处理所有已经收集到的数据,将所有(文件)缓冲区刷新到“磁盘”(持久内存)中,并且甚至还可能将数据上传到云端。
我根据这个答案编写了下面的代码来捕获每个关闭事件。(它只是一个测试代码。)
问题在于:如果我使用控制台右上角的 X 关闭程序,虽然终止序列仍在运行,但程序会在短暂延迟后被终止。(处理程序确实被调用,并开始等待线程加入,但之后还是会被杀掉。)如果我使用 Crt+C 或 Ctr+Break 终止程序,它将按预期工作;终止序列完成并退出该进程。
问题是:如何让操作系统等待我的应用程序终止,而不是在短暂的优雅期后杀死它?
我根据这个答案编写了下面的代码来捕获每个关闭事件。(它只是一个测试代码。)
问题在于:如果我使用控制台右上角的 X 关闭程序,虽然终止序列仍在运行,但程序会在短暂延迟后被终止。(处理程序确实被调用,并开始等待线程加入,但之后还是会被杀掉。)如果我使用 Crt+C 或 Ctr+Break 终止程序,它将按预期工作;终止序列完成并退出该进程。
问题是:如何让操作系统等待我的应用程序终止,而不是在短暂的优雅期后杀死它?
#region Trap application termination
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler;
enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static bool Handler(CtrlType sig, List<Thread> threads, List<Task> tasks, CancellationTokenSource cancellationRequest)
{
//starts new foregeound thread, so the process doesn't terminate when all the cancelled threads end
Thread closer = new Thread(() => terminationSequence(threads, tasks, cancellationRequest));
closer.IsBackground = false;
closer.Start();
closer.Join(); //wait for the termination sequence to finish
return true; //just to be pretty; this never runs (obviously)
}
private static void terminationSequence(List<Thread> threads, List<Task> tasks, CancellationTokenSource cancellationRequest)
{
cancellationRequest.Cancel(); //sends cancellation requests to all threads and tasks
//wait for all the tasks to meet the cancellation request
foreach (Task task in tasks)
{
task.Wait();
}
//wait for all the treads to meet the cancellation request
foreach (Thread thread in threads)
{
thread.Join();
}
/*maybe do some additional work*/
//simulate work being done
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine("Spinning");
while (stopwatch.Elapsed.Seconds < 30)
{
if (stopwatch.Elapsed.Seconds % 2 == 0)
{
Console.Clear();
Console.WriteLine("Elapsed Time: {0}m {1}s", stopwatch.Elapsed.Minutes, stopwatch.Elapsed.Seconds);
}
Thread.SpinWait(10000);
}
Environment.Exit(0); //exit the process
}
#endregion
static void Main(string[] args)
{
CancellationTokenSource cancellationRequest = new CancellationTokenSource(); //cancellation signal to all threads and tasks
List<Thread> threads = new List<Thread>(); //list of threads
//specifys termination handler
_handler += new EventHandler((type) => Handler(type, threads, new List<Task>(), cancellationRequest));
SetConsoleCtrlHandler(_handler, true);
//creating a new thread
Thread t = new Thread(() => logic(cancellationRequest.Token));
threads.Add(t);
t.Start();
}