2015-04-07 16 views
0

最近,我們在應用程序中遇到了崩潰問題(不是定期的,通常在客戶端),我們無法跟蹤錯誤的原因。大多數的我們從日誌獲取時間錯誤是這樣的..MVVM體系結構中的WPF應用程序中的應用程序崩潰問題


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(用於調用服務異步使用背景的)

PS:通常爲每日誌,我們緩存數據時出現此錯誤。

+0

它的indexOut外,還有不能忽視的,除非你的任何解決辦法處理它明確或固定它 – Muds

+4

望着堆棧跟蹤,它看起來像你的訪問列表''在多線程不適當的同步。您需要同步訪問權限。 –

回答

0

您的CurrentDomain_UnhandledException將處理Dispatcher(UI線程)中的異常。 BackgroundWorker是另一個線程,在此線程中發生的任何異常都不由Dispatcher處理,並且系統崩潰。

你應該在正確的線程,後臺處理IndexOutOfRangeException。

NG-HT