【问题标题】:DisconnectedContext was detected - Threadpool and Ping检测到 DisconnectedContext - 线程池和 Ping
【发布时间】:2013-12-11 19:56:54
【问题描述】:

我正在尝试创建一个多线程应用程序,该应用程序允许我 ping 数千台主机,ping 的结果将写入富文本框。

此应用程序执行后,一旦它遍历了一千个左右的地址,就会出现以下异常:

检测到DisconnectedContext 消息:此 RuntimeCallableWrapper 转换到 COM 上下文 0x4410e0 失败,出现以下错误:系统调用失败。 (来自 HRESULT 的异常:0x80010100 (RPC_E_SYS_CALL_FAILED))。这通常是因为创建此 RuntimeCallableWrapper 的 COM 上下文 0x4410e0 已断开连接,或者它正忙于做其他事情。从当前 COM 上下文(COM 上下文 0x440f70)释放接口。这可能会导致损坏或数据丢失。为避免此问题,请确保所有 COM 上下文/单元/线程保持活动状态并且可用于上下文转换,直到应用程序完全使用 RuntimeCallableWrapper 完成,该 RuntimeCallableWrapper 表示存在于其中的 COM 组件。

我不完全确定是什么原因造成的,起初我认为这是由于我没有处理 Ping,但我已经解决了这个问题,问题仍然存在。

如果有人有这方面的任何信息,将不胜感激。

谢谢大家。

public static void LogTextEvent(RichTextBox TextEventLog, Color TextColor, string EventText)
    {
        if (TextEventLog.InvokeRequired)
        {
            TextEventLog.BeginInvoke(new Action(delegate { LogTextEvent(TextEventLog, TextColor, EventText); }));
            return;
        }

        string nDateTime = DateTime.Now.ToString("hh:mm:ss tt") + " - ";

        // color text.
        TextEventLog.SelectionStart = TextEventLog.Text.Length;
        TextEventLog.SelectionColor = TextColor;

        // newline if first line, append if else.
        if (TextEventLog.Lines.Length == 0)
        {
            TextEventLog.AppendText(nDateTime + EventText);
            TextEventLog.ScrollToCaret();
            TextEventLog.AppendText(Environment.NewLine);
        }
        else
        {
            TextEventLog.AppendText(nDateTime + EventText + Environment.NewLine);
            TextEventLog.ScrollToCaret();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string[] logFile = File.ReadAllLines("addrs.txt");
        var addresses = new List<string>(logFile);

        foreach (string ip in addresses)
        {
            // See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem
            // for reason to use another variable in the loop
            string loopIp = ip;
            WaitCallback func = delegate
                {
                    if (PingIP(loopIp))
                    {
                        LogTextEvent(richTextBox1, Color.Green, "[ " + loopIp.ToUpper() + " ] - Ping Success");
                    }
                    else
                    {
                        LogTextEvent(richTextBox1, Color.Red, "[ " + loopIp.ToUpper() + " ] - Ping FAIL!");
                    }
                };

            ThreadPool.QueueUserWorkItem(func);
        }
    }

    public static bool PingIP(string IP)
    {
        bool result = false;
        var ping = new Ping();
        try
        {
            //var ping = new Ping();
            PingReply pingReply = ping.Send(IP);

            if (pingReply.Status == IPStatus.Success)
                result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            ping.Dispose();
        }
        return result;
    }

【问题讨论】:

  • 尝试使用一些合理的值调用 SetMaxThreads - 尝试 50。
  • 很遗憾没用,不过还是感谢您的建议。

标签: c# multithreading threadpool


【解决方案1】:

AndyDing 基本上是对的……问题在于 ScrollToCaret……这让我发疯了…… 我换了

rtbox.Select(box.Text.Length, 0);
rtbox.ScrollToCaret();

rtbox.Focus();
rtbox.Select(rtbox.Text.Length, 0);

问题已解决...在我的情况下,无法将 RichTextBox 换成 TextBox...需要不同的颜色/对齐方式 bla bla bla...但 AndyDing 让我走上了正确的道路。

干杯

【讨论】:

    【解决方案2】:

    我遇到了类似的“DisconnectedContext”故障,花了一天时间终于弄清楚问题是由RichTextBoxScrollToCaret()引起的。我用TextBox 替换了它,它会自动向下滚动,所以它甚至没有ScrollToCaret() 方法。幸运的是,我真的不需要RichTextBox 提供的那些额外功能,TextBox 在我的应用程序中就可以了。你可以试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多