简体   繁体   中英

C# Exception has been thrown by the target of an invocation

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:

Message:

Exception has been thrown by the target of an invocation.

Inner Exception Message:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Inner Exception Stack Trace:

at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.TextBoxBase.WndProc(Message& m) at System.Windows.Forms.RichTextBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& 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)
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
at TragTask.UserControls.CommentControl.set_Comment(TaskMilestoneTimerAndComment value) in C:\\TFS\\Tragging\\Tragging Solutions\\TragTask\\TragTask\\UserControls\\CommentControl.cs:line 49
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.<>c__DisplayClass11_0.b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c.<.cctor>b__8_0(Object state)

TargetSite

{System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)}

Target Site, Declaring Types:

{Name = "RuntimeMethodHandle" FullName = "System.RuntimeMethodHandle"}

Stack Trace:

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()
at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
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()

Update 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):

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();
    }
}

MSDN :

Corrupted process state exceptions are exceptions that indicate that the state of a process has been corrupted. It's not recommended to execute your application in this state.

By default, the common language runtime (CLR) does not deliver these exceptions to managed code, and the try/catch blocks (and other exception-handling clauses) are not invoked for them. If you are absolutely sure that you want to maintain your handling of these exceptions, you must apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method whose exception-handling clauses you want to execute. The CLR delivers the corrupted process state exception to applicable exception clauses only in methods that have both the HandleProcessCorruptedStateExceptionsAttribute and SecurityCriticalAttribute attributes.

You can also add the to your application's configuration file. This will ensure that corrupted state exceptions are delivered to your exception handlers without the HandleProcessCorruptedStateExceptionsAttribute or SecurityCriticalAttribute attribute. This configuration element has no effect on applications that were compiled in versions previous to the .NET Framework 4 but are running in the .NET Framework 4 or later; corrupted state exceptions will continue to be delivered for those applications. The HandleProcessCorruptedStateExceptionsAttribute attribute is ignored when it is encountered in partially trusted or transparent code, because a trusted host should not allow an untrusted add-in to catch and ignore these serious exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM