title: C# Exception has been thrown by the target of an invocation
body: <p>I'm having this error when running my C# .net 4.5 windows forms project from IDE or exe, I have no clear or fixed scenario, and I can't find a clue in the exception that is being thrown. I want to know how to get the source of error and fix it, or at least handle it in a way that does not make the exe crashed! 
  Below are the details of the exception:</p>
  
  <p><strong>Message:</strong></p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation.</p>
  </blockquote>
  
  <p><strong>Inner Exception Message:</strong></p>
  
  <blockquote>
    <p>Attempted to read or write protected memory.
    This is often an indication that other memory is corrupt.</p>
  </blockquote>
  
  <p><strong>Inner Exception Stack Trace:</strong></p>
  
  <blockquote>
    <p>at
    System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
    wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)    at
    System.Windows.Forms.NativeWindow.DefWndProc(Message&amp; m)    at
    System.Windows.Forms.Control.DefWndProc(Message&amp; m)    at
    System.Windows.Forms.Control.WndProc(Message&amp; m)    at
    System.Windows.Forms.TextBoxBase.WndProc(Message&amp; m)    at
    System.Windows.Forms.RichTextBox.WndProc(Message&amp; m)    at
    System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;
    m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
    hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at
    System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
    Int32 msg, Int32 wParam, EDITSTREAM lParam)    at
    System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)<br>
    at System.Windows.Forms.RichTextBox.StreamIn(String str, Int32 flags) 
    at System.Windows.Forms.RichTextBox.set_Rtf(String value)    at
    TragTask.UserControls.CommentControl.SetCommentInfo() in
    C:\TFS\Tragging\Tragging
    Solutions\TragTask\TragTask\UserControls\CommentControl.cs:line 76<br>
    at
    TragTask.UserControls.CommentControl.set_Comment(TaskMilestoneTimerAndComment
    value) in C:\TFS\Tragging\Tragging
    Solutions\TragTask\TragTask\UserControls\CommentControl.cs:line 49<br>
    at TragTask.Forms.frmTaskDetails.d__24.MoveNext()
    in C:\TFS\Tragging\Tragging
    Solutions\TragTask\TragTask\Forms\frmTaskDetails.cs:line 339    at
    System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object
    stateMachine)    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.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
    at
    System.Runtime.CompilerServices.TaskAwaiter.&lt;>c__DisplayClass11_0.b__0()
    at
    System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
    at
    System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.&lt;>c.&lt;.cctor>b__8_0(Object
    state)</p>
  </blockquote>
  
  <p><strong>TargetSite</strong></p>
  
  <blockquote>
    <p>{System.Object InvokeMethod(System.Object,
    System.Object[], System.Signature, Boolean)}</p>
  </blockquote>
  
  <p><strong>Target Site, Declaring Types:</strong> </p>
  
  <blockquote>
    <p>{Name = "RuntimeMethodHandle" FullName = "System.RuntimeMethodHandle"}</p>
  </blockquote>
  
  <p><strong>Stack Trace:</strong></p>
  
  <blockquote>
    <p>at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
    arguments, Signature sig, Boolean constructor)    at
    System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,
    Object[] parameters, Object[] arguments)    at
    System.Delegate.DynamicInvokeImpl(Object[] args)    at
    System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry
    tme)    at
    System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
    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.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state)    at
    System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry
    tme)    at System.Windows.Forms.Control.InvokeMarshaledCallbacks()<br>
    at System.Windows.Forms.Control.WndProc(Message&amp; m)    at
    System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;
    m)    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
    msg, IntPtr wparam, IntPtr lparam)    at
    System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)<br>
    at
    System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr
    dwComponentID, Int32 reason, Int32 pvLoopData)    at
    System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
    reason, ApplicationContext context)    at
    System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
    reason, ApplicationContext context)    at
    System.Windows.Forms.Application.Run()    at TragTask.Program.Main()</p>
  </blockquote>
  
  <p><strong>Update</strong>
  After C.Evenhuis pointed out that this has to do with CommentControl.SetCommentInfo method, I thought it would be needed to add this method in my question (note that tb_comment is a RichTextBox control):</p>
  
  <pre><code>public bool SetCommentInfo()
  {
      try
      {
          this.SuspendLayout();
  
          lbl_user.Text = "userName";
          lbl_dateTime.Text = mComment.DateTimeAdded.ToString("dd/MM/yyyy HH:mm:ss");
  
          if (mComment.Comment.TrimStart().StartsWith("{\\rtf1", StringComparison.Ordinal))
          {
              tb_comment.Clear();
              tb_comment.Text = "";
              tb_comment.Rtf = mComment.Comment;
          }
          else
              tb_comment.Text = mComment.Comment;
  
          return true;
      }
      catch (Exception ex)
      {
          Utils.Global.ErrorLog("SetCommentInfo", ex, true);
          return false;
      }
      finally
      {
          this.ResumeLayout();
      }
  }
  </code></pre>