c# - 如何避免强制终止以停止控制台应用程序?
问题描述
我需要能够按需优雅地关闭控制台应用程序。
使用这篇文章,我能够在收到 CTRL 事件时控制退出。但是,我无法使用 taskkill 按需停止应用程序。
如果我尝试命令:
taskkill /im myapp
它回应:
错误:无法终止 PID 为 23296 的进程“MyApp.exe”。原因:这个进程只能被强制终止(使用 /F 选项)。
解决方案
基于添加到这篇文章的许多贡献以及检测控制台应用程序退出在 c 中,我构建了以下帮助程序,以便在 taskkill 信号上优雅地停止控制台应用程序,同时也停止任何其他关闭控制信号:
public class StopController : Form, IMessageFilter
{
//logger
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static private CancellationTokenSource cancellationTokenSource;
public StopController(CancellationTokenSource cancellationTokenSource)
{
StopController.cancellationTokenSource = cancellationTokenSource;
System.Windows.Forms.Application.AddMessageFilter(this);
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 16)
{
log.Warn("Receiveing WF_Close event. Cancel was fired.");
cancellationTokenSource.Cancel();
}
return true;
}
public static void Activate(CancellationTokenSource cancellationTokenSource)
{
Task.Run(() => Application.Run(new StopController(cancellationTokenSource)));
}
#region unmanaged
//must be static.
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
// Put your own handler here
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
log.Warn("CTRL+C received!. Cancel was fired.");
cancellationTokenSource.Cancel();
break;
case CtrlTypes.CTRL_BREAK_EVENT:
log.Warn("CTRL+BREAK received!. Cancel was fired.");
cancellationTokenSource.Cancel();
break;
case CtrlTypes.CTRL_CLOSE_EVENT:
log.Warn("Program being closed!. Cancel was fired.");
cancellationTokenSource.Cancel();
break;
case CtrlTypes.CTRL_LOGOFF_EVENT:
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
log.Warn("User is logging off!. Cancel was fired.");
cancellationTokenSource.Cancel();
break;
default:
log.Warn($"unknow type {ctrlType}");
break;
}
return true;
}
// Declare the SetConsoleCtrlHandler function
// as external and receiving a delegate.
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
#endregion
}
用法:
StopController.Activate(cancellationTokenSource);
无需更改为 windows 应用程序。
推荐阅读
- java - 无法使用 savedInstanceState 在 SearchView 上设置可见性
- xaml - 如何在 UWP XAML 中双向绑定用户控件?(C++/WinRT)
- python - 分组日期时间的期间列表
- python - 如何通过单个操作截断数据帧的系列/列
- java - 将变量写入 response.varfile
- c# - 如何在xaf中实现idatastore接口
- angular - Angular Bootstrap ngbtimepicker 无法设置模型
- angular - 我们如何从 Angular 6 中基于另一个数组的数组中删除对象
- amazon-web-services - 使用 curl 获取 S3 文件的 MD5 值
- javascript - 默认自动隐藏表格内容