title: Locating the source of an UnhandledException
body: <p>My program seems to have a bug in it which I'm finding hard to track down. The program is multithreaded and quite complex. In a nutshell it communicates with multiple clients using asynchronous sockets and writes to the database.</p>
  
  <p>A System.Reflection.TargetInvocationException is thrown when I hammer the program with client communications and hold down the enter key to spam writes into the database. At some point in time the exception is thrown which I'm catching with the following snippet of code in the constructor of the main window:</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>I don't have much to work with but I was wondering if someone could suggest some tools or or point me in the right direction to help me detect the location of this exception. The exception in detail is pasted below.</p>
  
  <pre><code>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&amp; 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&amp; handled)
         at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; 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&amp; 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:
  </code></pre>