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 9173077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:30:18+00:00 2026-06-17T16:30:18+00:00

I am working on a file listener (for txt files) using FileSystemWatcher and want

  • 0

I am working on a file listener (for txt files) using FileSystemWatcher and want to append all new lines to a RichTextBox. Format new text to rich text needs alot of cpu power, so I decided to use a BackgroundWorker for this.

The program is still working, but form hangs when I lock MS Windows and relogin. Seems as BackgroundWorker thread is blocking the UI thread. There is no other effect of using BeginInvoke instead of Invoke for AppendText to RichTextBox.

When I pass the unformatted text to e.Result of bw_DoWork everything is working fine (no blocking threads). The temporary RichTextBox is not attached to the main form. How can I solve the “hang” problem?

Logic before code sample: FileSystemWatcher event Changed starts BackgroundWorker.RunWorkerAsync.

StringBuilder unformattedText;

private void bw_DoWork( object sender, DoWorkEventArgs e )
{
    // Write all new lines for this file to 
    // StringBuilder variable unformattedText
    ReadFromFile (...)

    // Need a temporary RichTextBox to create new formatted text
    RichTextBox tempRtb = new RichTextBox();

    // Split new unformatted text with regex, format some tokens 
    // and append each to temporary RichTextBox tempRtb
    HighlightWords( tempRtb, unformattedText );

    e.Result = tempRtb.Rtf;
}

private void bw_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
    string rtfText = e.Result as string;

    // Append text to main RichTextBox 
    if (rtb.InvokeRequired)
    {
            rtb.BeginInvoke(
                new MethodInvoker( delegate
                {
                    AppendRichText( rtb, rtfText );
                } ) );
    }
    else
    { 
        AppendRichText( rtb, rtfText );
    }
}

private void AppendRichText(RichTextBox rtb, string text)
{
    // Append formatted text at the end of the RichTextBox
    rtb.Select( rtb.TextLength, 0 );
    rtb.SelectedRtf = text;

    rtb.ScrollToCaret();
}

Note: These are only the code parts to understand the problem. It’s not complete.

  • 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-06-17T16:30:19+00:00Added an answer on June 17, 2026 at 4:30 pm

    The RichTextBox cannot be created or manipulated on the worker thread. You MUST do it on the UI thread.

    The bw_RunWorkerCompleted is run on the UI thread, so BeginInvoke is not required.

    You need to move all the code after ReadFromFile into bw_RunWorkerCompleted and you will not need a temp RichTextBox anymore.

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

Sidebar

Related Questions

I have a working file rename java tool, now I want to add an
I working on adding file uploading to my web application. I'm using an iframe
I'm working on a file format that should be written and read in several
I am working with a file (fasta file), here is the format- >chr1 AACCCCCCCCTCCCCCCGCTTCTGGCCACAGCACTTAAACACATCTCTGC
So I'm working with a file format. The file format contains chunks of data...
I am working on a project where I want to add a click listener
Am working on file / folder event capturing of OS X 10.5 and 10.6
On whatever reason this is not working (says 'file not found'), set in=c:\myprogram\_save cd
This is my first time working with file i/o in java, and it's not
I am working on pom file modifications to change the version node value. I

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.