2012-02-08 62 views
2

我的程序似乎有一個錯誤,我很難找到它。該程序是多線程的,相當複雜。簡而言之,它使用異步套接字與多個客戶端通信並寫入數據庫。查找UnhandledException的來源

當我使用客戶端通信敲擊程序並按住Enter鍵將垃圾郵件寫入數據庫時​​,System.Reflection.TargetInvocationException被拋出。在某個時間點發生異常時,我正在與下面的代碼片段在主窗口的構造醒目:

VMMainWindow() 
    { 
     ... 
     System.Windows.Threading.Dispatcher.CurrentDispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(CurrentDispatcher_UnhandledException); 
     .... 
    } 

    static void CurrentDispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) 
    { 
     string str = e.ToString(); 
    } 

我沒有多少與工作,但我想知道如果有人可以建議一些工具或者或者指向正確的方向來幫助我檢測這個異常的位置。下面粘貼詳細例外。

System.Reflection.TargetInvocationException was unhandled Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: 
     at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
     at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
     at System.Delegate.DynamicInvokeImpl(Object[] args) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
     at System.Threading.ExecutionContext.runTryCode(Object userData) 
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Windows.Threading.DispatcherOperation.Invoke() 
     at System.Windows.Threading.Dispatcher.ProcessQueue() 
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.Run() 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at SupernovaServer.App.Main() in D:\Projects\Supernova Server\Server\obj\x86\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() InnerException: System.ArgumentOutOfRangeException 
     Message=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
     Source=mscorlib 
     ParamName=index 
     StackTrace: 
      at System.ThrowHelper.ThrowArgumentOutOfRangeException() 
      at System.Collections.Generic.List`1.get_Item(Int32 index) 
      at System.Collections.ObjectModel.Collection`1.System.Collections.IList.get_Item(Int32 index) 
      at System.Windows.Data.ListCollectionView.AdjustBefore(NotifyCollectionChangedAction action, Object item, Int32 index) 
      at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 
      at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
     InnerException: 
+0

ArgumentOutOfRangeException表明它將成爲評估某處的linq表達式。看起來像它可能包含您的代碼的唯一文件是App.g.cs.我會看看那個班,並檢查我的列表不是空的。 – jhsowter 2012-02-08 03:10:43

+0

@jhsowter - ArgumentOutOfRangeException如何顯示它與linq有關?我認爲這是一個通用的例外? 我看過App.g.cs,但裏面沒有任何列表 – n00b 2012-02-08 04:27:48

+0

這是一個典型的linq相關異常的異常下的堆棧跟蹤。對不起,應該更清楚了。 – jhsowter 2012-02-08 05:20:07

回答

4

是否有可能有多個線程正在對並非線程安全的集合進行併發訪問?說,一個線程刪除一個項目,而另一個正在閱讀或添加?這肯定會導致這種類型的錯誤。通常,.NET集合類(System.Collections.Concurrent中的那些除外)不是線程安全的。您可以擁有多個閱讀器,但對集合的任何修改都需要獨佔訪問權限 - 通過鎖定或讀寫器鎖定進行保護。

+0

嗨吉姆,我認爲這是問題所在。我切換到一個安全的線程集合,我沒有看到它經過一些壓力測試後崩潰。然而,我有一個問題 - 在我進行任何添加/刪除之前,我在集合周圍有獨佔的鎖(..){}語句之前,我注意到程序會掛在看起來像死鎖的東西上。當我在鎖中使用一個OBJECT時,這可能嗎?即圍繞所有集合操作鎖定(OBJECT) – n00b 2012-02-08 05:07:50

+0

除非您擁有多個集合的鎖定,否則獨佔的「鎖定」語句訪問無法創建死鎖情況。您可以進入線程1擁有鎖A並且想要獲取鎖B的情況,並且線程2具有鎖B並且想要獲取鎖A.這是死鎖的令人痛心的常見原因。但我不知道你可以在一個鎖上死鎖。 – 2012-02-08 16:49:59

1

這是很安全的假設App.g.cs不是問題所在,所以就像你說的,沒有太多去。所以這是我的提示:看看內部的異常。這些東西非常方便,我敢打賭,你至少有一層內在的例外。

+0

也有一些是在內部異常但它似乎並沒有成爲我的代碼,因爲它是一個「System.Collections.ListDictionaryInternal」內部集合 [System.ArgumentOutOfRangeException] \t {「索引超出範圍。必須爲非負並小於集合的大小\ r \ n參數名:索引「} \t System.ArgumentOutOfRangeException 數據\t {System.Collections.ListDictionaryInternal} \t System.Collections.IDictionary {System.Collections.ListDictionaryInternal} 來源\t「mscorlib」 – n00b 2012-02-08 04:19:35