DisconnectedContext was detected - Threadpool and Ping

3

I'm attempting to create a multi-threaded application which will allow me to ping thousands of hosts, the results of the ping are written to a richtextbox.

After this application executes, once it's iterated through a thousand or so addresses, I'm presented with the following exception:

DisconnectedContext was detected Message: Transition into COM context 0x4410e0 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x4410e0 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x440f70). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.

I'm not entirely sure what's causing this, I at first figured it was due to my not disposing Ping but I've since addressed that and the problem still persists.

If anyone has any information on this it would be greatly appreciated.

Thank you all.

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;
    }
c#
multithreading
threadpool
asked on Stack Overflow Dec 11, 2013 by Clu

2 Answers

3

AndyDing is mostly right... problem is with ScrollToCaret... this drove me mad... I swapped

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

with

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

problem solved... In my situation swapping RichTextBox for TextBox was not possible... need different colors/alignment bla bla bla... but AndyDing got me on the right path.

Cheers

answered on Stack Overflow May 8, 2014 by JCElzinga • edited May 8, 2014 by Robert
1

I encountered the similar "DisconnectedContext" failure, and spent one day to finally figure out the problem is induced by ScrollToCaret() of RichTextBox. I replaced it with a TextBox, which automatically scrolls down, so it even doesn't have a ScrollToCaret() method. Fortunately I don't really need those extra features provided by RichTextBox, a TextBox is just fine in my application. You can give a try.

answered on Stack Overflow Feb 9, 2014 by AndyDing • edited Feb 9, 2014 by ignotusverum

User contributions licensed under CC BY-SA 3.0