控制台激活关闭事件
問(wèn)題:服務(wù)器上放的控制臺(tái)程序很容易被別人或自己誤操作關(guān)關(guān)閉,那程序正在處理操作的數(shù)據(jù)可能正處理到一般,這個(gè)時(shí)候數(shù)據(jù)庫(kù)里面的數(shù)據(jù)可能只是整個(gè)業(yè)務(wù)的中間狀態(tài),不是我們要的最終結(jié)果,咋辦呢??
解決辦法:
1.主備控制臺(tái),切換到備胎程序,檢測(cè)中間業(yè)務(wù)狀態(tài)的數(shù)據(jù),加入到處理中(業(yè)務(wù)狀態(tài)通常會(huì)有點(diǎn)復(fù)雜,改起來(lái)會(huì)比較煩,合適業(yè)務(wù)狀態(tài)少,業(yè)務(wù)簡(jiǎn)單的場(chǎng)景),即使主機(jī)斷點(diǎn),從機(jī)依然能處理數(shù)據(jù)
2.激活關(guān)閉窗口事件,暫停循環(huán)處理數(shù)據(jù)的程序,線程打盹3秒,等待將當(dāng)前數(shù)據(jù)處理完成,對(duì)于斷電斷網(wǎng)宕機(jī)事件就沒(méi)辦法了,這里有個(gè)問(wèn)題,3秒能處理的完嗎?有點(diǎn)慌...或者說(shuō)我10毫秒就處理完了,你讓我等3秒那么久。。。
下面是2的例子:
?
class Program{//實(shí)例化Timer類(lèi) private static System.Timers.Timer aTimer = new System.Timers.Timer();#region 激活關(guān)閉窗口事件public delegate bool ControlCtrlDelegate(int CtrlType);[DllImport("kernel32.dll")]private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);/// <summary>/// 關(guān)閉窗口時(shí)的事件/// </summary>/// <param name="CtrlType"></param>/// <returns></returns>static bool HandlerRoutine(int CtrlType){Console.WriteLine("關(guān)閉窗口事件被激活");Console.WriteLine("do something...");//停止定時(shí)掃描aTimer.Enabled = false;System.Threading.Thread.Sleep(3000);Console.WriteLine("可以關(guān)閉了");//System.Threading.Thread.Sleep(3000);return false;}#endregionstatic void Main(string[] args){//注冊(cè)窗口關(guān)閉事件bool bRet = SetConsoleCtrlHandler(cancelHandler, true);aTimer.Elapsed += new ElapsedEventHandler(TaskBegin);aTimer.Interval = 1000;aTimer.AutoReset = true;//執(zhí)行一次 false,一直執(zhí)行true //是否執(zhí)行System.Timers.Timer.Elapsed事件 aTimer.Enabled = true;#region 防止自動(dòng)關(guān)閉var key = string.Empty;while (key != "E"){System.Threading.Thread.Sleep(3000);key = Console.ReadLine().ToUpper();}#endregion}private static void TaskBegin(object source, System.Timers.ElapsedEventArgs e){Console.WriteLine("任務(wù)開(kāi)始執(zhí)行");}}
?
?
?
?對(duì)于2中等待3秒做個(gè)優(yōu)化:
思路:關(guān)閉事件中通知數(shù)據(jù)消費(fèi)中心終止消費(fèi),并循環(huán)判斷數(shù)據(jù)消費(fèi)中心當(dāng)前消費(fèi)的那條數(shù)據(jù)消費(fèi)結(jié)束沒(méi),結(jié)束了跳出循環(huán),關(guān)閉控制臺(tái)程序
代碼應(yīng)該是下面這個(gè)樣子:
class Program{//實(shí)例化Timer類(lèi) private static System.Timers.Timer aTimer = new System.Timers.Timer();#region 激活關(guān)閉窗口事件public delegate bool ControlCtrlDelegate(int CtrlType);[DllImport("kernel32.dll")]private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);/// <summary>/// 關(guān)閉窗口時(shí)的事件/// </summary>/// <param name="CtrlType"></param>/// <returns></returns>static bool HandlerRoutine(int CtrlType){Console.WriteLine("關(guān)閉窗口事件被激活");Console.WriteLine("do something...");//停止定時(shí)掃描//aTimer.Enabled = false;while (Stop!=true){Console.WriteLine("當(dāng)前flag:"+flag);System.Threading.Thread.Sleep(1000);}return false;}#endregion[STAThread]static void Main(string[] args){//注冊(cè)窗口關(guān)閉事件bool bRet = SetConsoleCtrlHandler(cancelHandler, true);aTimer.Elapsed += new ElapsedEventHandler(TaskBegin);aTimer.Interval = 1000;aTimer.AutoReset = true;//執(zhí)行一次 false,一直執(zhí)行true //是否執(zhí)行System.Timers.Timer.Elapsed事件 aTimer.Enabled = true;#region 防止自動(dòng)關(guān)閉var key = string.Empty;while (key != "E"){System.Threading.Thread.Sleep(3000);key = Console.ReadLine().ToUpper();}#endregion}private static int flag = 1;private static bool Stop = false;private static void TaskBegin(object source, System.Timers.ElapsedEventArgs e){Console.WriteLine("任務(wù)開(kāi)始執(zhí)行");Console.WriteLine("線程:"+Thread.CurrentThread.ManagedThreadId);flag++;if (flag > 20){Stop = true;}Console.WriteLine("flag:"+ flag);}}
轉(zhuǎn)載于:https://www.cnblogs.com/liuqiyun/p/9566062.html
總結(jié)
- 上一篇: 当我们在谈论multidex65535时
- 下一篇: django之jquery完成ajax