Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5841053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:50:19+00:00 2026-05-22T11:50:19+00:00

Greetings, I’m having problem invoking a richTextBox from a worker thread in C#. I’m

  • 0

Greetings, I’m having problem invoking a richTextBox from a worker thread in C#. I’m using the InvokeRequired/Invoke method. Please see my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void ThreadSafe(MethodInvoker method)
    {
        if (InvokeRequired)
            Invoke(method);
        else
            method();
    }

    private void WorkerThread(object data)
    {
        string msg = "\nhello, i am thread " + data.ToString();
        ThreadSafe(delegate
        {
            richTextBox1.AppendText(msg);
        });
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Thread[] workers = new Thread[3];

        for (int i = 0; i < 3; i++)
        {
            workers[i] = new Thread(WorkerThread);
            workers[i].Start(i);
            string msg = "\nthread " + i.ToString() + "started!";
            richTextBox1.AppendText(msg);
        }

        int j = 3;
        while (j > 0)
        {
            for (int i = 0; i < 3; i++)
            {
                Thread.Sleep(250);
                richTextBox1.AppendText("\nChecking thread");
                if (workers[i].Join(250))
                {
                    string msg = "\nWorker thread " + i.ToString() + " finished.";
                    richTextBox1.AppendText(msg);
                    workers[i] = null;
                    j--;    // decrement the thread watch count
                }
            }
        }
    }
}

It prints the following in the rich text box:

thread 0started!
thread 1started!
thread 2started!
Checking thread
Checking thread
Checking thread
Checking thread
....

It goes on and on, the “hello” message doesn’t get printed, and the UI is frozen.
Then I changed Invoke() to BeginInvoke(), which I know I shouldn’t do, then the result is like this:

thread 0started!
thread 1started!
thread 2started!
Checking thread
Worker thread 0 finished.
Checking thread
Worker thread 1 finished.
Checking thread
Worker thread 2 finished.
hello, i am thread 0
hello, i am thread 1
hello, i am thread 2

What is the reason and what do I do?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:50:20+00:00Added an answer on May 22, 2026 at 11:50 am

    You are waiting for the worker threads to complete before leaving your button1_Click method. This is a problem because your threads can’t complete until they are able to run the delegate that calls the AppendText method on that same thread.

    When you change to BeginInvoke you don’t have this problem any more because your worker threads don’t block waiting on the delegate that calls the AppendText method.

    If you are trying to block the UI thread until the worker threads complete you simply will not be able to as long as they rely on the call to Invoke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I'm trying to find a way to 'unbind' a socket from a particular
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings, I have a particular object which can be constructed from a file, as
Greetings, In an WPF DataGridTemplateColumn I have a CellTemplate using a ListView and a
Greetings Everyone I am new in asp.net and i'm using RadControls for Asp.net Ajax
Greetings All, I need to optimize a RegEx I am using to parse template
Greetings all, I've been told by co-workers that the GET method is different in
Greetings. I'm looking for a way to parse a number of XML files in
Greetings! I'm working on wrapping my head around LINQ. If I had some XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.