是否有可能有一个C#类多个定时器一个例外处理程序? [英] Is it possible to have one Exception handler for multiple timers in a C# class?

查看:246
本文介绍了是否有可能有一个C#类多个定时器一个例外处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行在系统托盘中的应用程序C#程序 - 转让/移动/创建/在后台编辑文件

I have a C# program which runs a system tray application - transferring / moving / creating / editing files in the background.

有是一个很大的异常处理和循环处理,以防止从崩溃/卡住如果用户手册中删除一个文件的程序正在与该程序

There is alot of exception handling and loop handling to prevent the program from crashing / getting stuck if the user manual deletes a file the program is working with.

在程序运行上具有计算机的不幸1该程序崩溃。计算机是很难得到,而且已经不能装上调试软件(这是一个嵌入式PC以外,没有网络访问)。

Unfortunately one of the computer the program is running on is having the program crash. The computer is very hard to get, and cannot have debugging software loaded on (it is an embedded PC outside with no network access).

我试图找出原因崩溃(如未处理exeption),但是没有发现任何东西,并且没有获得,因为它是在远程位置的PC。

I have tried finding the cause of the crash (such as an unhandled exeption) however have not found anything and do not have access to the PC as it is in a remote location.

我希望能找到一种方法使用的AppDomain /一个UnhandledExceptionEventHandler捕捉所有未处理的异常并记录他们的诊断。
但是我已经(故意)在类DoSomething.class里面的办公室产生的异常,都撞碎了WinForm应用程序,而不是记录的异常错误。

I was hoping to find a way to use AppDomain / an UnhandledExceptionEventHandler to catch all unhandled exceptions and log them for diagnosis. However the exception I have (deliberately) created in the office inside the class "DoSomething.class", are crashing the WinForm application rather than logging the exception error.

我的代码是:对未处理的异常处理
的AppDomain currentDomain

My code is:

    //Current domain for the unhandled exception handling
    AppDomain currentDomain;

    [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
    public MainForm()
    {
        InitializeComponent();

        currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);


        CreateTimer.Interval = Settings.Default.CreateTimer;
        CreateTimer.Start();
        RunCopyProtocol();

        ChangeFilesTimer.Interval = 10000;
        ChangeFilesTimer.Start();
        RunChangeFiles();

        throw new Exception("ABC");

    }

    public void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
    {
        try
        {
            Exception ex = (Exception)args.ExceptionObject;
            SetLabel(ex);
        }
        finally { }
    }

    private string SetLabel(Exception ex)
    {
        String str;

        str = ex.Message;
        Label1.Text = str;

        return str;
    }

    private void ChangeFilesTimer_Tick(object sender, EventArgs e)
    {
        RunChangeFiles();
        throw new Exception("2");
    }



为什么不抛出新的异常调用未处理的异常错误?
我将如何使用AppDomain中得到未处理的异常处理程序来处理这个异常的RunChangeFiles异常?

代码的应用程序域是基于/的 MSDN例子

Code on AppDomain is based of MSDN examples

推荐答案

如果您的计时器是 System.Timers.Timer 的原因被记录通过MSDN 这里

If your timer is a System.Timers.Timer the reason is documented by MSDN here:

定时器组件渔获物和抑制由事件处理程序经过的事件引发的所有异常。

The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event.

在这个类似的问题请看:
如何得到发生在计时器事件经过的异常?

Take a look at this similar question: How do I get the Exception that happens in Timer Elapsed event?

您必须赶上那些在抛出的异常。经过处理,并重新抛出它们放在线程池线程

You'll have to catch the exceptions that are thrown in the elapsed handler, and rethrow them on a ThreadPool thread.

使用上面的代码和扩展从引用问题的答案:

Using your code above and extending the answer from the referenced question:

private void ChangeFilesTimer_Tick(object sender, EventArgs e)
{
    try
    {
        RunChangeFiles();
    }
    catch (Exception ex)
    {
        ThreadPool.QueueUserWorkItem(
            _ => { throw new Exception("Exception on timer thread.", ex); });
    }
}






如果你的定时器是一个 System.Windows.Forms.Timer 那么你将需要挂接到的 Application.ThreadException 事件来处理未处理的异常。


If your timer is a System.Windows.Forms.Timer then you will need to hook into the Application.ThreadException event to handle unhandled exceptions.

订阅这个事件调用 Application.Run()

您也可以重新抛出异常之前办理例外在当地异常处理块的记录。

You can also handle logging of the Exception in a local exception handling block before rethrowing the exception.

try
{
    /// ...
}
catch (Exception ex)
{
    if (ex is ArgumentException)
    {
        /// handle known or specific exceptions here
    }
    else 
    {
        /// log then rethrow unhandled exceptions here
        logExceptions(ex);
        throw;  
    }
}

这篇关于是否有可能有一个C#类多个定时器一个例外处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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