崩溃事件C# [英] Crash Event C#

查看:92
本文介绍了崩溃事件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是很陌生,我很好奇,当应用程序崩溃时是否可以调用一个事件?我有一个程序编写和格式化.csv文件以供商店库存使用,但是其中一些计算机充满错误(更不用说我自己对C#的新手知识)会导致一些崩溃,从而导致人们失去工作.因此,我想编写一个在崩溃时调用的事件,该事件将其当前工作的副本保存到其我的文档"文件夹中.如果没有这样做的事情,您能给我一种方法做些什么吗?

I''m fairly new to C# and I was curious, is there an event I can call when an application crashes? I have a program that writes and formats .csv files for use in store inventory, but some of these computers are riddled with errors (Not to mention my own noviceness with C#) are causing a few crashes, which causes people to lose work. So, I wanted to write an event to be called upon crash that saves a copy of their current work to their My Documents folder. If there is no event that does this, could you please give me a way to sorta go about doing this? Thanks!

推荐答案

您可以看到此链接->

单击
You can see this link->

Click


我''m猜猜这是一个Winforms应用程序吗?

您可以设置一些全局"错误处理程序,以捕获您在代码中未明确处理的所有异常.

在您的应用程序启动时,按照以下代码编写一些代码:

I''m guessing this is a Winforms application?

You could set up some ''global'' error handlers that will trap any exceptions you haven''t explicitly handled in your code.

In your application startup, have some code along the lines of

/// <summary>
/// Main thread exception handler
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">event</param>
public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{
}

/// <summary>
/// Application domain exception handler
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">event</param>
public static void AppDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
}

[STAThread]
public static void Main()
{
    // Unhandled exceptions for our Application Domain
    AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(AppDomain_UnhandledException);

    // Unhandled exceptions for the executing UI thread
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    // Whatever else your application does on startup

}



在事件存根中,编写一些代码,这些代码将记录异常详细信息或执行任何操作

可以从某些异常中恢复,但是有些异常会导致应用程序崩溃....最好进行调试,找出导致问题的原因!



In the event stubs, write some code that will either log the exception details or do whatever

Some exceptions can be recovered from, but some will cause an application crash....best get debugging and find out what is causing the problem!


没有真正的事件导致应用程序崩溃(除了此处其他一些答案中提到的事件之外)-当应用程序崩溃时,它已经停止工作,因此所有进一步的执行都停止了.

在这种情况下,您需要使用WinDbg之类的工具或任何其他调试器.
There is no real event for an application crash (other than events mentioned in some of the other answers here) - when an application crashes, it has stopped working, so all further execution stops.

You need to use a tool like WinDbg or any other debugger in such cases.


这篇关于崩溃事件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆