第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

捕獲控制臺退出C#

捕獲控制臺退出C#

30秒到達(dá)戰(zhàn)場 2019-06-26 16:52:29
捕獲控制臺退出C#我有一個控制臺應(yīng)用程序,其中包含了相當(dāng)多的線程。有些線程監(jiān)視某些條件,如果它們是真,則終止程序。這種終止可以在任何時候發(fā)生。我需要一個可以在程序關(guān)閉時觸發(fā)的事件,以便清理所有其他線程并正確關(guān)閉所有文件句柄和連接。我不確定.NET框架中是否已經(jīng)內(nèi)置了一個,所以我在編寫自己的框架之前先問一下。我想知道是否有這樣的活動:MyConsoleProgram.OnExit += CleanupBeforeExit;
查看完整描述

3 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗 獲得超2個贊

完整的示例,使用ctrl-c,用X關(guān)閉窗口并殺死:

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;using System.Threading;namespace TestTrapCtrlC {
    public class Program {
        static bool exitSystem = false;

        #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) {
            Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");

            //do your cleanup here
            Thread.Sleep(5000); //simulate some cleanup delay

            Console.WriteLine("Cleanup complete");

            //allow main to run off
            exitSystem = true;

            //shutdown right away so there are no lingering threads
            Environment.Exit(-1);

            return true;
        }
        #endregion

        static void Main(string[] args) {
            // Some boilerplate to react to close window event, CTRL-C, kill, etc
            _handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(_handler, true);

            //start your multi threaded program here
            Program p = new Program();
            p.Start();

            //hold the console so it doesn’t run off the end
            while (!exitSystem) {
                Thread.Sleep(500);
            }
        }

        public void Start() {
            // start a thread and start doing some processing
            Console.WriteLine("Thread started, processing..");
        }
    }}


查看完整回答
反對 回復(fù) 2019-06-26
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗 獲得超13個贊

有WinForms應(yīng)用程序;

Application.ApplicationExit += CleanupBeforeExit;

對于控制臺應(yīng)用程序,請嘗試

AppDomain.CurrentDomain.DomainUnload += CleanupBeforeExit;

但是我不確定在什么時候調(diào)用它,或者它是否在當(dāng)前域中工作。我懷疑不是。


查看完整回答
反對 回復(fù) 2019-06-26
  • 3 回答
  • 0 關(guān)注
  • 875 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號