程序崩溃时自动退出?

发布于 2024-11-14 23:11:09 字数 226 浏览 1 评论 0原文

有没有办法让程序在崩溃时停止并退出?我使用 C# 来开发程序,运行它的机器是 32 位 Windows 7 Professional 机器。

例如,当我的 X.exe 崩溃时,会出现一个带有 2 个选项的小弹出窗口: 1 - 在线寻找解决方案。 2 - 停止 X.exe

我希望自动选择第二个选项并停止我的程序。我有另一个程序监视 X.exe,因此当 X.exe 停止时,它将启动 X.exe 的新实例。

Is there any way to make a program stop and exit the program when it crashes? I am using C# to develop the program and machines it is run on are 32 bit Windows 7 Professional machines.

So, for example, when my X.exe crashes, a little pop-up window with 2 options come up:
1 - Look for solutions online.
2 - Stop X.exe

I want the second option to be automatically selected and my program is stopped. I have another program that monitors X.exe so when X.exe is stopped, it will start a new instance of X.exe.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小耗子 2024-11-21 23:11:09

您可以为未处理的异常添加全局异常处理程序。当您的程序在运行时未处理异常时,总是会调用此处理程序,这将导致程序崩溃:

在 Wpf-Applications 中,您可以像这样添加处理程序:

App.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler( Current_DispatcherUnhandledException );

WinForms 相同:

System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler( OnThreadException );

对于控制台应用程序:

System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException );

You can add a global exception handler for unhandled exceptions. This handler is always called when your program did not handle an exception during runtime which will cause the programm to crash:

In Wpf-Applications you can add the handler like this:

App.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler( Current_DispatcherUnhandledException );

The same for WinForms:

System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler( OnThreadException );

And for console applications:

System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException );
披肩女神 2024-11-21 23:11:09

此代码应该为您指明方向:

public static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // catch app errors
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    try
    {
        Application.Run(new Form());
    }
    catch (Exception exc)
    {
        // show popupform
        PopupForm popup = new PopupForm();
        if(popup.ShowDialog() == DialogResult.OK)
        {
            Application.Restart();
        }
        else
        {
            Application.Exit();
        }
    }
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // show popupform
    PopupForm popup = new PopupForm();
    if(popup.ShowDialog() == DialogResult.OK)
    {
        Application.Restart();
    }
    else
    {
        Application.Exit();
    }
}

Lg warappa

This code should guide you the way:

public static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // catch app errors
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    try
    {
        Application.Run(new Form());
    }
    catch (Exception exc)
    {
        // show popupform
        PopupForm popup = new PopupForm();
        if(popup.ShowDialog() == DialogResult.OK)
        {
            Application.Restart();
        }
        else
        {
            Application.Exit();
        }
    }
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // show popupform
    PopupForm popup = new PopupForm();
    if(popup.ShowDialog() == DialogResult.OK)
    {
        Application.Restart();
    }
    else
    {
        Application.Exit();
    }
}

Lg warappa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文