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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:39:01+00:00 2026-05-26T12:39:01+00:00

I have a project coded in .NET Winforms. I need to implement a data-mining

  • 0

I have a project coded in .NET Winforms.
I need to implement a data-mining operation, print the text to TextBox and update the progress.

I tried to use BackgroundWorker to do, but it throws a InvalidOperationException (Cross-thread operation not valid: Control ‘xxxxx’ accessed from a thread other than the thread it was created on)

To narrow down the potential causes of the problem, I started a new project, included the following:
Button – To start the BackgroundWorker
Label – to print the text.
And ProgressBar.

However, the result is the same. I searched on SOF, and was told to use a delegate, but I am not familiar with it.

This is the code sample that throws the error:

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace TestProject
{
    public partial class Form1 : Form
    {
        private readonly BackgroundWorker _bw = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();
            _bw.DoWork += RosterWork;
            _bw.ProgressChanged += BwProgressChanged;
            _bw.RunWorkerCompleted += BwRunWorkerCompleted;
            _bw.WorkerReportsProgress = true;
            _bw.WorkerSupportsCancellation = false;
        }

        private void RosterWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            for (int i = 0; i < 1000; i++)
            {
                label1.Text = i.ToString();
                _bw.ReportProgress(Convert.ToInt32((i * (100 / 1000))));
            }
        }

        private void BwProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            progressBar1.Show();
            _bw.RunWorkerAsync();
        }

        private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Hide();
        }

    }
}

Update:
I follow Jon Skeet’s answer, it really work on my test project, but back to my real project,

The Layout of my Form:

Form –
TabControl
– Tab1
-Tab1Panel
-TextBox1

When reach this line :

TextBox txtbox1 = new TextBox();
Tab1Panel.Controls.Add(txtbox1);

The error still occur when i add Textbox to Panel Control Programmatically.

Finally,I replace by this:

 if (Tab1Panel.InvokeRequired)
     Tab1Panel.Invoke((MethodInvoker)delegate { Tab1Panel.Controls.Add(txtbox1); });
 else
     Tab1Panel.Controls.Add(txtbox1);

Everything is work. How to determine the control is InvokeRequired, Is it control specified?

  • 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-26T12:39:01+00:00Added an answer on May 26, 2026 at 12:39 pm

    This is the problem:

    label1.Text = i.ToString();
    

    You’re trying to change the label text within the BackgroundWorker, which is not running on a UI thread. The point of BackgroundWorker is to do all the non-UI work there, using ReportProgress to periodically “go back” to the UI thread and update the UI with the progress you’re making.

    So either you need to change label1.Text in BwProgressChanged as well, or you need to use Control.Invoke/BeginInvoke just as you would from any other background thread:

    // Don't capture a loop variable in a lambda expression...
    int copy = i;
    Action updateLabel = () => label1.Text = copy.ToString();
    label1.BeginInvoke(updateLabel);
    

    For more about the copying part, see Eric Lippert’s blog post, “Closing over the loop variable considered harmful”. In this particular case it’s only an issue because I’m using BeginInvoke. This could be changed to just:

    Action updateLabel = () => label1.Text = i.ToString();
    label1.Invoke(updateLabel);
    

    … but now the background worker would always be waiting for the UI to catch up before it kept going, which in real life is usually not what you want. I generally prefer BeginInvoke over Invoke.

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

Sidebar

Related Questions

I have an existing C# 3.0 WinForms project with .NET 3.5 that talks to
I'm using agsXMPP in my .NET project (WinForms) I have the following Code block,
I have the following code in my ASP.NET project public sealed class IoC {
We have a software house developing code for us on a project, .NET Web
I have a master page in my asp.net MVC project, which has code like
I have a directory named reports inside my winform project in .net. My project
We have a project that needs to gather survey data. On one particular page
I'm using CC.Net 1.4.4.83 to build my project. I also have some scheduled tasks
I have created a project with a simple RDLC report in ASP.NET which when
In a C# WinForms, .NET Framework 3.5, project with a WebBrower control on the

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.