gpt4 book ai didi

调用的目标已抛出 C# 异常

转载 作者:行者123 更新时间:2023-12-03 08:12:04 25 4
gpt4 key购买 nike

从 IDE 或 exe 运行我的 C# .net 4.5 windows 窗体项目时出现此错误,我没有明确或固定的方案,并且在引发的异常中找不到线索。我想知道如何获取错误源并修复它,或者至少以不会使 exe 崩溃的方式处理它!
以下是异常的详细信息:

留言:

Exception has been thrown by the target of an invocation.



内部异常消息:

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



内部异常堆栈跟踪:

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)



目标网站

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



目标站点,声明类型:

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



堆栈跟踪:

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



更新
在 C.Evenhuis 指出这与 CommentControl.SetCommentInfo 方法有关后,我认为需要在我的问题中添加此方法(注意 tb_comment 是 RichTextBox 控件):
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 :

损坏的进程状态异常是指示进程状态已损坏的异常。不建议在这种状态下执行您的应用程序。

默认情况下,公共(public)语言运行时 (CLR) 不会将这些异常传递给托管代码,并且不会为它们调用 try/catch block (和其他异常处理子句)。如果您绝对确定要保持对这些异常的处理,则必须将 HandleProcessCorruptedStateExceptionsAttribute 属性应用于要执行其异常处理子句的方法。 CLR 仅在同时具有 HandleProcessCorruptedStateExceptionsAttribute 和 SecurityCriticalAttribute 属性的方法中将损坏的进程状态异常传递给适用的异常子句。

您还可以将其添加到应用程序的配置文件中。这将确保在没有 HandleProcessCorruptedStateExceptionsAttribute 或 SecurityCriticalAttribute 属性的情况下将损坏的状态异常传递给您的异常处理程序。此配置元素对在 .NET Framework 4 之前的版本中编译但在 .NET Framework 4 或更高版本中运行的应用程序没有影响;将继续为这些应用程序提供损坏的状态异常。当在部分受信任或透明代码中遇到 HandleProcessCorruptedStateExceptionsAttribute 属性时,它会被忽略,因为受信任的主机不应允许不受信任的加载项捕获并忽略这些严重异常。

关于调用的目标已抛出 C# 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37882736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com