MVVM 架构中的 WPF 应用程序中的应用程序崩溃问题
Application Crash issue in WPF application in MVVM architecture
最近,我们的应用程序遇到了崩溃问题(不是定期发生,通常是在客户端),我们无法追踪错误的原因。大多数时候我们从日志中得到的错误是这样的..
Type : System.IndexOutOfRangeException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Index was outside the bounds of the array.
Source : mscorlib
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void Add(T)
HResult : -2146233080
Stack Trace : at System.Collections.Generic.List`1.Add(T item)
System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
另外,为了在应用程序级别处理崩溃,我们在 App.xaml.cs
文件中编写了某些代码,即
private void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
try
{
var exception = (Exception) e.ExceptionObject;
ExceptionHandler.HandleException(exception);
WpfMessageBox.Show(LocalizationManager.GetValue("AppErrorUnhandledException").ToString(),
LocalizationManager.GetValue("AppErrorUnhandledException").ToString(),
WpfMessageBoxButtons.OK,
WpfMessageBoxIcon.Error);
}
除此之外,我们还需要做些什么来阻止应用程序突然崩溃吗?
注意:我们的应用是基于MVVM模式,服务外观,统一,服务编排,MutiThreading(使用后台调用服务异步)等概念
P.S:通常根据日志,我们在缓存数据时遇到此错误。
您的 CurrentDomain_UnhandledException 将处理来自 UI 线程 Dispatcher 的异常。
BackgroundWorker是另外一个线程,在这个线程中发生任何异常都不会被Dispatcher处理,你的系统会崩溃。
您应该在正确的线程(后台线程)中处理 IndexOutOfRangeException。
ng-HT
最近,我们的应用程序遇到了崩溃问题(不是定期发生,通常是在客户端),我们无法追踪错误的原因。大多数时候我们从日志中得到的错误是这样的..
Type : System.IndexOutOfRangeException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Index was outside the bounds of the array.
Source : mscorlib
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void Add(T)
HResult : -2146233080
Stack Trace : at System.Collections.Generic.List`1.Add(T item)
System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
另外,为了在应用程序级别处理崩溃,我们在 App.xaml.cs
文件中编写了某些代码,即
private void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
try
{
var exception = (Exception) e.ExceptionObject;
ExceptionHandler.HandleException(exception);
WpfMessageBox.Show(LocalizationManager.GetValue("AppErrorUnhandledException").ToString(),
LocalizationManager.GetValue("AppErrorUnhandledException").ToString(),
WpfMessageBoxButtons.OK,
WpfMessageBoxIcon.Error);
}
除此之外,我们还需要做些什么来阻止应用程序突然崩溃吗?
注意:我们的应用是基于MVVM模式,服务外观,统一,服务编排,MutiThreading(使用后台调用服务异步)等概念
P.S:通常根据日志,我们在缓存数据时遇到此错误。
您的 CurrentDomain_UnhandledException 将处理来自 UI 线程 Dispatcher 的异常。 BackgroundWorker是另外一个线程,在这个线程中发生任何异常都不会被Dispatcher处理,你的系统会崩溃。
您应该在正确的线程(后台线程)中处理 IndexOutOfRangeException。
ng-HT