有没有办法阻止.NET控制台应用程序被关闭?我有一个遵循这种模式的应用程序:
while (true)
{
string x = Console.ReadLine();
StartLongRunningTaskOnSeparateThread(x);
}
Run Code Online (Sandbox Code Playgroud)
问题是可以关闭控制台窗口(因此切断长时间运行的任务).控制台应用程序是否存在Forms.OnClosing事件的等效项?
编辑 - 我不是想创造一些不可能杀人的东西,也许不会发出警告信息e..g"嘿,我还没有完成.你确定要关闭我吗?"
EDIT2 - 通过"x"按钮防止不合时宜的退出比阻止Ctrl-C(我使用过Console.TreatControlCAsInput = true;)更重要.假设为此目的,任何启动任务管理器的人都想要杀死程序,以至于他们应该能够.并且最终用户宁愿看到警告而不是意外取消他们长时间运行的任务.
bea*_*ach 10
这是我尝试解决这个问题.任务管理器仍然可以关闭应用程序.但是,我的想法是尝试检测何时关闭然后重新启动它.但是,我没有实施那部分.
以下程序将检测到CTRL-C && CTRL-BREAK并将继续运行.
编辑:删除"X"按钮
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace DoNotCloseMe
{
class Program
{
const string _title = "DO NOT CLOSE - Important Program";
static void Main(string[] args)
{
Console.Title = _title;
IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle;
IntPtr hSystemMenu = GetSystemMenu(hMenu, false);
EnableMenuItem(hSystemMenu, SC_CLOSE, MF_GRAYED);
RemoveMenu(hSystemMenu, SC_CLOSE, MF_BYCOMMAND);
WriteConsoleHeader();
//This function only seems to be called once.
//After calling MainLoop() below, CTRL-C && CTRL-BREAK cause Console.ReadLine() to return NULL
Console.CancelKeyPress += (sender, e) =>
{
Console.WriteLine("Clean-up code invoked in CancelKeyPress handler.");
Console.WriteLine("Key Pressed: {0}", e.SpecialKey.ToString());
System.Threading.Thread.Sleep(1000);
MainLoop();
// The application terminates directly after executing this delegate.
};
MainLoop();
}
private static void MainLoop()
{
while (true)
{
WriteConsoleHeader();
string x = Console.ReadLine();
if (!String.IsNullOrEmpty(x))
{
switch (x.ToUpperInvariant())
{
case "EXIT":
case "QUIT":
System.Environment.Exit(0);
break;
default:
StartLongRunningTaskOnSeparateThread(x);
break;
}
}
}
}
private static void StartLongRunningTaskOnSeparateThread(string command)
{
var bg = new System.ComponentModel.BackgroundWorker();
bg.WorkerReportsProgress = false;
bg.WorkerSupportsCancellation = false;
bg.DoWork += (sender, args) =>
{
var sleepTime = (new Random()).Next(5000);
System.Threading.Thread.Sleep(sleepTime);
};
bg.RunWorkerCompleted += (sender, args) =>
{
Console.WriteLine("Commmand Complete: {0}", command);
};
bg.RunWorkerAsync();
}
private static void WriteConsoleHeader()
{
Console.Clear();
Console.WriteLine(new string('*', Console.WindowWidth - 1));
Console.WriteLine(_title);
Console.WriteLine(new string('*', Console.WindowWidth - 1));
Console.WriteLine("Please do not close this program.");
Console.WriteLine("It is maintaining the space/time continuum.");
Console.WriteLine("If you close it, Q will not be happy and you will be assimilated.");
Console.WriteLine(new string('*', Console.WindowWidth - 1));
Console.WriteLine("Development Mode: Use \"EXIT\" or \"QUIT\" to exit application.");
Console.WriteLine(new string('*', Console.WindowWidth - 1));
}
#region "Unmanaged"
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern IntPtr RemoveMenu(IntPtr hMenu, uint nPosition, uint wFlags);
internal const uint SC_CLOSE = 0xF060;
internal const uint MF_GRAYED = 0x00000001;
internal const uint MF_BYCOMMAND = 0x00000000;
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)