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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:13:49+00:00 2026-06-06T19:13:49+00:00

// sample 1 use BackgroundWorker, it works fine private BackgroundWorker workerPorcee = new BackgroundWorker();

  • 0
    // sample 1 use BackgroundWorker, it works fine
    private BackgroundWorker workerPorcee = new BackgroundWorker();
    private void button1_Click(object sender, EventArgs e)
    {
        workerPorcee.DoWork += new DoWorkEventHandler(workerPorcee_DoWork);
        workerPorcee.ProgressChanged += new ProgressChangedEventHandler(workerPorcee_ProgressChanged);
        workerPorcee.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerPorcee_RunWorkerCompleted);
        workerPorcee.WorkerReportsProgress = true;
        workerPorcee.RunWorkerAsync();
    }
    void workerPorcee_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.progressBar1.Value = e.ProgressPercentage;
    }
    void workerPorcee_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            workerPorcee.ReportProgress(i);
            Thread.Sleep(100);
        }
    }

    void workerPorcee_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.progressBar1.Value = 100;
    }

I just implement my worker thread, but it will get the exception: Cross-thread operation not valid: Control ‘progressBar1’ accessed from a thread other than the thread it was created on.

    // my worker thread
     private void button3_Click(object sender, EventArgs e)
    {
        Worker wk = new Worker();
        Thread thr = new Thread(new ThreadStart(wk.ProcessEvent));
        wk.ProgressChanged += new delCallback(wk_ProgressChanged);
        thr.Start();
    }        

    void wk_ProgressChanged(int percent)
    {
        this.progressBar1.Value = percent;
    //exception:Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.       

     }

     public delegate void delCallback(int percent);

public class Worker
{
    public event delCallback ProgressChanged;

    public void ProcessEvent()
    {
        for (int i = 0; i < 100; i++)
        {
            if (ProgressChanged != null)
            {
                this.ProgressChanged(i);
            }
            Thread.Sleep(100);
        }
    }
}

WinForm, I just want to use my worker thread to set the progress bar value, but it fail. Any one help to point what is the issue? How to implement the functionality just like BackgroundWorker ?

  • 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-06T19:13:51+00:00Added an answer on June 6, 2026 at 7:13 pm

    You need to ensure that all interaction with winForms controls is performed on the main thread that created the control – that is what the exception is telling you. To do this you need to use the Control.Invoke method which will make sure the call to your UI-altering method is performed on the correct thread.

    So, in your code example you need to change it to something like:

    void wk_ProgressChanged(int percent)
    {
        if(this.progressBar1.InvokeRequired)
        {
            this.progressBar1.Invoke(wk_ProgressChanged, new Object[] {percent});
        }
        else
        {
            this.progressBar1.Value = percent;
        }       
     }
    

    The first part checks to see if the method call is being performed on the correct Thread for the progressBar1 control. If we are on a different thread, Invoke is used to call the same method but on the correct thread.
    Invoke will then call the wk_ProgressChanged method, but this time InvokeRequired will be false (as we are now running on the correct thread to perform the UI update), and the progressBar1 value is set.

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

Sidebar

Related Questions

I am trying to use some sample code and my compiler won't compile this
i've read this answer but i don't know how to use that sample in
How can you use the following sample data either with a foreach or while
Are there any sample applications avaliable that make use of both caliburn and prism?
I am looking for sample code or documentation on how to use client side
Does MS have a sample enterprise application that demonstrates the use of different Enterprise
I want to use URL such as /Image/sample.png I create route, but it does
I have the following sample code and noticed that if I attempt to use
Here's sample model classes, which being use with Entity Framework Code First: public class
In a sample use of the BeginInvoke thread pool method: ... Func<string, int> method

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.