是否有可能在 c# WPF 应用程序中全局捕获意外错误 - c# 4.0

发布于 2024-12-11 11:38:33 字数 230 浏览 0 评论 0原文

是否有可能在 c# WPF 应用程序中全局捕获意外错误 - c# 4.0

我发现 DispatcherUnhandledException 能够捕获 UI 线程错误,但实际上我需要 TPL 线程。 UnhandledException 能够捕获线程错误,但它仍然导致软件终止。

因此,任何解决方案都可以在线程中而不是 UI 线程中捕获未处理的异常,并且仍然让软件运行而不终止。线程是 TPL 线程。 (任务并行库)

Is it possible to catch unexpected errors globally in c# WPF application - c# 4.0

I found that DispatcherUnhandledException was able to catch UI thread errors but actually i need TPL threads. UnhandledException was able to catch thread errors but it was still causing software to terminate.

So any solution to catch unhandled exceptions in the threads not in UI thread and still let software to run not terminate. Threads are TPL threads. (Task Parallel Library)

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

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

发布评论

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

评论(2

╄→承喏 2024-12-18 11:38:33

您已经完成的处理 DispatcherUnhandledException 的一部分将其添加到您的配置文件中,

<configuration>
  <runtime>  
    <legacyUnhandledExceptionPolicy enabled="1"/>
  </runtime>
</configuration>

这可以防止辅助线程异常关闭应用程序。

A part from handling DispatcherUnhandledException which you already have done add this to your config file

<configuration>
  <runtime>  
    <legacyUnhandledExceptionPolicy enabled="1"/>
  </runtime>
</configuration>

This prevents your secondary threads exception from shutting down the application.

歌入人心 2024-12-18 11:38:33

您可以使用 TPL 中的自定义升级策略来解决您的问题。您可以通过向静态 System.Threading.Tasks.TaskScheduler.UnobservedTaskException 成员添加事件处理程序来实现此目的

    class Test
    {
        static void Main(string[] args)
        {
            // create the new escalation policy
            TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs eventArgs) =>
             {
                // mark the exception as being handled
                eventArgs.SetObserved();
                // get the aggregate exception and process the contents
                 ((AggregateException)eventArgs.Exception).Handle(ex =>
                    {
                        // write the type of the exception to the console
                         Console.WriteLine("Exception type: {0}", ex.GetType());
                        return true;
                    });
              };
            // create tasks that will throw an exception
            Task task1 = new Task(() =>
            {
                throw new NullReferenceException();
            });
            Task task2 = new Task(() =>
            {
                throw new ArgumentOutOfRangeException();
            });
            // start the tasks
            task1.Start(); task2.Start();
            // wait for the tasks to complete - but do so
            // without calling any of the trigger members
            // so that the exceptions remain unhandled
            while (!task1.IsCompleted || !task2.IsCompleted)
            {
                Thread.Sleep(500);
            }
            // wait for input before exiting
            Console.WriteLine("Press enter to finish and finalize tasks");
            Console.ReadLine();
        }
    }
}

You can use Custom Escalation Policy in TPL to address your case.You do this by adding an event handler to the static System.Threading.Tasks.TaskScheduler.UnobservedTaskException member

    class Test
    {
        static void Main(string[] args)
        {
            // create the new escalation policy
            TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs eventArgs) =>
             {
                // mark the exception as being handled
                eventArgs.SetObserved();
                // get the aggregate exception and process the contents
                 ((AggregateException)eventArgs.Exception).Handle(ex =>
                    {
                        // write the type of the exception to the console
                         Console.WriteLine("Exception type: {0}", ex.GetType());
                        return true;
                    });
              };
            // create tasks that will throw an exception
            Task task1 = new Task(() =>
            {
                throw new NullReferenceException();
            });
            Task task2 = new Task(() =>
            {
                throw new ArgumentOutOfRangeException();
            });
            // start the tasks
            task1.Start(); task2.Start();
            // wait for the tasks to complete - but do so
            // without calling any of the trigger members
            // so that the exceptions remain unhandled
            while (!task1.IsCompleted || !task2.IsCompleted)
            {
                Thread.Sleep(500);
            }
            // wait for input before exiting
            Console.WriteLine("Press enter to finish and finalize tasks");
            Console.ReadLine();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文