Thread: New to threading, trouble with delegates

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    6

    New to threading, trouble with delegates

    Hi. I'm working on a chat program, and I'm having some trouble with the thread that is supposed to get text from a file and append it to a rich text box that acts as the chat display. The problem arises when I attempt to edit the textbox text from the thread, I get this error:
    Code:
    Control 'richTextBox1' accessed from a thread other than the thread it was created on.
    Here is the thread's function:
    Code:
    private void Thread_get_text()
            {
                string text = "";
                string new_text = "";
    
                while (true)
                {
                    StreamReader read = new StreamReader("chat.log");
                    new_text = read.ReadLine();
                    if (text != new_text)
                    {
                        text = new_text;
                        read.Close();
                        richTextBox1.Text = text;
                    }
                    read.Close();
                    Thread.Sleep(10);
                }
            }
    Now, I understand that I'm supposed to use a delegate here, but I don't quite understand how to use one. I looked at MSDN's delegate tutorial and followed their example, and I ended up with this:

    Code:
    //declaration
    private delegate void Del(string text);
    
    //AppendText definition
    private void AppendText(string text)
    {
          richTextBox1.Text = text;
    }
    
    private void Thread_get_text()
            {
                Del handler = AppendText;
                string text = "";
                string new_text = "";
    
                while (true)
                {
                    StreamReader read = new StreamReader("chat.log");
                    new_text = read.ReadLine();
                    if (text != new_text)
                    {
                        text = new_text;
                        read.Close();
                        handler(text);
                    }
                    read.Close();
                    Thread.Sleep(10);
                }
            }
    This prompts the same error message: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

    What did I do wrong with the delegate? Is this even the correct approach to fixing my problem?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You need to Invoke when you are on a different thread than the main UI thread.

    http://msdn.microsoft.com/en-us/libr...ol.invoke.aspx

    or

    http://msdn.microsoft.com/en-us/libr...gininvoke.aspx
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    As prog-bman says, you can't cross thread a GUI control. 2 useful functions are:

    Code:
    rtb.Invoke()
    rtb.BeginInvoke()
    Lots of demos on google and msdn.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    Awesome. Thanks for the quick responses, guys. I'll check it out.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Note that you might want to put a try/catch block around the Invoke method since in some cases (like when you are terminating threads when the main windows closes) they will throw an exception. Just an empty try/catch will do the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  5. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM