Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控件上的AccessViolationException。调用_C#_Access Violation - Fatal编程技术网

C# 控件上的AccessViolationException。调用

C# 控件上的AccessViolationException。调用,c#,access-violation,C#,Access Violation,我的设置如下。我有一个带有日志属性的UI线程,可以更新RichTextBox。我有一个填充日志属性的非UI线程。非UI线程由计时器控制。我在UI中还有一个更新日志属性的按钮。LogPresenter只是一个格式化消息并将其发送到Log属性的类 更新RichTextBox上的SelectedText属性时,我随机收到一个AccessViolationException。这里是相关的代码 public static class ThreadHelpers { public static vo

我的设置如下。我有一个带有日志属性的UI线程,可以更新RichTextBox。我有一个填充日志属性的非UI线程。非UI线程由计时器控制。我在UI中还有一个更新日志属性的按钮。LogPresenter只是一个格式化消息并将其发送到Log属性的类

更新RichTextBox上的SelectedText属性时,我随机收到一个AccessViolationException。这里是相关的代码

public static class ThreadHelpers
{
    public static void InvokeIfRequired(this Control c, Action a)
    {
        if (c.InvokeRequired)
            c.Invoke(a);
        else
            a();
    }
}

public string Log
{
    set
    {
        lock (value)
        {
            richLog.InvokeIfRequired(() => OnLogMessage(value));
        }                
    }
}

[HandleProcessCorruptedStateExceptions]
private void OnLogMessage(string message)
{
    try
    {
        richLog.SelectionStart = richLog.Text.Length;

        var selection = message.Split('-');
        var loggingLevel = selection[0].Trim();

        switch (loggingLevel)
        {
            case "Information":
                richLog.SelectionColor = Color.SteelBlue;
                break;
            case "Warning":
                richLog.SelectionColor = Color.Goldenrod;
                break;
            case "Error":
                richLog.SelectionColor = Color.DarkRed;
                break;
            case "SuccessAudit":
                richLog.SelectionColor = Color.DarkOliveGreen;
                break;
            case "FailureAudit":
                richLog.SelectionColor = Color.OrangeRed;
                break;
            default:
                richLog.SelectionColor = Color.Silver;
                break;
        }

        richLog.SelectedText = message;
        ScrollToBottom();
     }
     catch (Exception ex)
     {
        // The lines below just cause cascading fail so I took them out.
        //_logPresenter.Fail("Failed to write Log: {0}", message);
        //_logPresenter.Error("{0}" + Environment.NewLine + "StackTrace: {0}", ex.Message, ex.StackTrace);
     }
 }
我得到的确切错误是: System.AccessViolationException异常

试图读取或写入受保护的内存。这通常表示其他内存已损坏

   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_SelectedText(String value)
   at Namespace.InteractiveServiceForm.OnLogMessage(String message) in c:\projects\Namespace\InteractiveServiceForm.cs:line 127

我尝试了锁,但它没有帮助任何建议将不胜感激。此外,该应用程序现在刚刚挂起UI。

Add Debug.Assertc.IsHandleCreated作为invokeFrequeUIRedokay的第一行,我在中添加了Debug.Assert。我正在等待应用程序再次遇到问题。当我启动应用程序时,消息框会弹出。我会在它再次崩溃后再联系你。你在做你无法兑现的承诺。你真的能处理损坏的状态异常吗?如果你这么做了,你就不会问这个问题了。你真的能抓住每一个异常并处理它们吗?别撒谎了,你会有机会调试这个的。迷你转储可以告诉你它在哪里掉了,它不在托管代码中。@HansPassant这不是最有建设性的评论。我已经在互联网上到处找过了,我还在继续找。我不知道我躺在哪里。我唯一能想到你会认为我是的地方是当我说我会在Zer0再次崩溃后回到它身边。但它会再次崩溃,我只是不知道什么时候。我假设Zer0让我包含Debug.Assert,因为他/她想知道我出错的原因是否是因为句柄丢失。如果你不想帮忙,那就不要回应。好吧,没花那么长时间。我解释了如何调试这个。如果你认为这是一种非建设性的方式,那么祝你好运,你需要它。操作系统代码中的AVE和它们来的一样令人讨厌。