C# 控制台应用程序 - 如何判断应用程序何时关闭?

发布于 2024-09-25 18:43:39 字数 1697 浏览 2 评论 0原文

所以我有一个程序需要在退出之前进行一些清理,即使它是强制退出。我知道,按下电源按钮是强制退出,无法进行清理,但这也会导致其他更大的问题。

该程序挂钩许多事件来负责清理工作。我的问题是,在调试模式下编译时,这不能正常工作;在发布模式下编译并从 VS (F5) 中运行时,这不能正常工作。在发布模式下,从控制台或文件管理器运行,一切都会按预期进行。

当尝试调试应用程序时,如果处理程序例程被触发,我会收到一个不错的对话框,显示 vshost 已失败,正在寻找问题的解决方案...

我需要做什么才能使此代码在调试和发布模式下工作?

编辑

错误:错误

具体步骤如下: 1)按F5 2)等一下 3) 单击控制台窗口中的[X] 4)错误,参见上面的链接图片

class Program
{
    public enum CtrlTypes
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT,
        CTRL_CLOSE_EVENT,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

    static string conDB = "";

    [DllImport("Kernel32")]
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
    public delegate bool HandlerRoutine(CtrlTypes CtrlType);
    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
    {
        DatabaseAccess db = DatabaseAccess.Instance; //Issues Here

        // Cleanup code removed

        return true;
    }

    public static void ExitProcess(object sender, EventArgs args)
    {
#if DEBUG
        Debug("\n\n\nDebug Mode\nPress any key to continue...\n");
        Console.ReadKey(true);
#endif
    }

    static int Main(string[] args)
    {
        SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
        AppDomain.CurrentDomain.ProcessExit += ExitProcess;

        // App code removed

        return 0;
    } // End main


    static void Debug(string text)
    {
        #if DEBUG
            Console.WriteLine(text);
        #endif
    }
}

So i have a program that needs to do some cleanup before it exits, even if it is a forced exit. I know, hitting the power button is a forced exit where no cleanup can happen, but that would cause other, bigger issues too.

The Program hooks into a number of events to take care of the cleanup. My issue is that this does not work properly when compiled in Debug mode and this does not work correctly when compiled in Release mode and run from within VS (F5). In release mode, run from the console or from the file manager, everything does what it is supposed to.

When trying to debug the application if the Handler routine is triggered I get a nice dialog saying vshost has failed, looking for solution to the problem...

What do I have to do to make this code work in debug and release mode?

EDIT

Error: Error

exact steps take:
1) Press F5
2) wait a seconds
3) Click the [X] in the console window
4) Error, see linked image above

class Program
{
    public enum CtrlTypes
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT,
        CTRL_CLOSE_EVENT,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

    static string conDB = "";

    [DllImport("Kernel32")]
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
    public delegate bool HandlerRoutine(CtrlTypes CtrlType);
    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
    {
        DatabaseAccess db = DatabaseAccess.Instance; //Issues Here

        // Cleanup code removed

        return true;
    }

    public static void ExitProcess(object sender, EventArgs args)
    {
#if DEBUG
        Debug("\n\n\nDebug Mode\nPress any key to continue...\n");
        Console.ReadKey(true);
#endif
    }

    static int Main(string[] args)
    {
        SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
        AppDomain.CurrentDomain.ProcessExit += ExitProcess;

        // App code removed

        return 0;
    } // End main


    static void Debug(string text)
    {
        #if DEBUG
            Console.WriteLine(text);
        #endif
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文