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

  • Home
  • SEARCH
  • 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 535957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:46:18+00:00 2026-05-13T09:46:18+00:00

I have an application wherein two threads write asynchronously to a single textbox. It

  • 0

I have an application wherein two threads write asynchronously to a single textbox. It works, except that the second thread to write to the textbox overwrites the line that the first thread just wrote. Any thoughts or insight into the problem would be greatly appreciated. I am using Microsoft Visual C# 2008 Express Edition. Thanks.

  delegate void SetTextCallback(string text);

  private void SetText(string text)
  {
     this.textBox1.Text += text;
     this.textBox1.Select(textBox1.Text.Length, 0);
     this.textBox1.ScrollToCaret();
  }

  private void backgroundWorkerRx_DoWork(object sender, DoWorkEventArgs e)
  {
     string sText = "";

     // Does some receive work and builds sText

     if (textBox1.InvokeRequired)
     {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { sText });
     }
     else
     {
        SetText(sText);
     }
  }
  • 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-13T09:46:18+00:00Added an answer on May 13, 2026 at 9:46 am

    EDIT: This might not solve the problem, but you might want to handle the ProgressChanged event of the BackgroundWorkers and set the text there.

    For example:

    void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
        SetText((string)e.UserState);
    }  //Make this method handle the RunWorkerCompleted for both workers
    
    //In DoWork:
        worker.ReportProgress(0, sText);
    

    ProgressChanged is fired on the UI thread, so you don’t need to call Invoke.

    By the way, you should probably rename SetText to AppendText to make the code clearer.
    Also, you can use the built-in delegate Action<String> instead of making your own SetTextCallback delegate type.

    EDIT: Also, you should probably move the InvokeRequired check to SetText.

    For example:

    private void AppendText(string text) {
        if(textBox1.InvokeRequired) {
            textBox1.Invoke(new Action<string>(AppendText), text);
            return;
        }
        this.textBox1.AppendText(text);
        this.textBox1.SelectionStart = textBox1.TextLength;
        this.textBox1.ScrollToCaret();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application wherein the application will need to check whether one user
i have an application wherein i have incorporate a Remember Me feature for the
i have an application wherein i am accessing the Session object in the CommonCode.cs
i have an application wherein i am using a web-service. Now the ip address
Within my Rails application, I'd like to generate requests that behave identically to genuine
I have a framework that allows users to do queries to a specific datasource
I'm working on a navigation-based iPhone-only app that serves two main purposes: One, to
I have a site where, via Android, a user needs to: fill in a
Preface : I am new to the iPhone SDK, Obj-C, Interface Builder and Cocoa.

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.