gpt4 book ai didi

c# - 如何防止导致应用程序崩溃的异常?

转载 作者:行者123 更新时间:2023-11-30 23:12:41 29 4
gpt4 key购买 nike

我有一个 C# .NET 4.5.2 Windows 窗体应用程序,它是使用任务和等待架构实现的。最近我在应用程序中看到一个奇怪的行为。尽管我使用了很多 try-catch 语句并且几乎每种异常都被处理了几次(每 3 或 4 天),但我收到了 X 停止工作消息,但应用程序中没有任何日志。

我在事件查看器中只有两个条目,第一个是 .NET 运行时类型,第二个是应用程序错误。

对于日志记录,我使用 serilog 记录到控制台和滚动文件。出于远程通信目的,我使用 Slack(我认为这是问题的根源)。

日志内容如下:

    Application: X.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at SlackAPI.RequestState`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].GotResponse(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CompleteCallback(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Net.ContextAwareResult.Complete(IntPtr)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
at System.Net.HttpWebRequest.SetResponse(System.Exception)
at System.Net.HttpWebRequest.SetAndOrProcessResponse(System.Object)
at System.Net.ConnectionReturnResult.SetResponses(System.Net.ConnectionReturnResult)
at System.Net.Connection.CompleteConnectionWrapper(System.Object, System.Object)
at System.Net.PooledStream.ConnectionCallback(System.Object, System.Exception, System.Net.Sockets.Socket, System.Net.IPAddress)
at System.Net.ServicePoint.ConnectSocketCallback(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.Complete(IntPtr)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Faulting application name: X.exe, version: 1.5.0.0, time stamp: 0x5914053a
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x0655a904
Faulting process id: 0x1bb0
Faulting application start time: 0x01d2ca214de3853b
Faulting application path: PATH-TO-X.exe
Faulting module path: unknown
Report Id: 5ef2a70f-387a-11e7-9c66-000c29bbc8ee

为了与 Slack 通信,我使用 SlackAPI .有什么建议吗?

更新如何处理线程和任务未处理的异常。在program.cs Main函数中放一个全局的try catch对不对?

最佳答案

重点是您应该在应用启动后立即定义处理程序。

// Bind your handler asap aap starts
AppDomain.CurrentDomain.UnhandledException += GlobalUnhandledException;

// Gereftish ;)
static void GlobalUnhandledException(object sender, UnhandledExceptionEventArgs e) { // here we have the exception }

关于c# - 如何防止导致应用程序崩溃的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962446/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com