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

The Archive Base Latest Questions

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

i copied the following example Microsoft Thread Example Which gives the code below but

  • 0

i copied the following example Microsoft Thread Example

Which gives the code below
but i get an error on the line “this.progressBar1.Value = newval;” stating that “Cross-thread operation not valid: Control ‘progressBar1’ accessed from a thread other than the thread it was created on.”

what could be the issue?
thanks
damo

C# Code

public partial class Form1 : Form
{         
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Thread trd = new Thread(new ThreadStart(this.ThreadTask));
        trd.IsBackground = true;
        trd.Start();
    }
    private void ThreadTask()
    {
        int stp;
        int newval;
        Random rnd = new Random();
        while (true)
        {
            stp = this.progressBar1.Step * rnd.Next(-1, 2);
            newval = this.progressBar1.Value + stp;
            if (newval > this.progressBar1.Maximum)
                newval = this.progressBar1.Maximum;
            else if (newval < this.progressBar1.Minimum)
                newval = this.progressBar1.Minimum;
            this.progressBar1.Value = newval;
            Thread.Sleep(100);
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("This is the main thread");
    }
}
  • 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-15T02:18:18+00:00Added an answer on June 15, 2026 at 2:18 am

    You have to invoke new delegate:

        delegate void ThreadTaskDelegate();
        private void ThreadTask()
        {
            if (this.InvokeRequired)
            {
                ThreadTaskDelegate del = new ThreadTaskDelegate(ThreadTask);
                this.Invoke(del, null);
            }
            else
            {
                int stp;
                int newval;
                Random rnd = new Random();
                while (true)
                {
                    stp = this.progressBar1.Step * rnd.Next(-1, 2);
                    newval = this.progressBar1.Value + stp;
    
                    if (newval > this.progressBar1.Maximum)
                        newval = this.progressBar1.Maximum;
                    else if (newval < this.progressBar1.Minimum)
                        newval = this.progressBar1.Minimum;
    
                    this.progressBar1.Value = newval;
    
                    Thread.Sleep(100);
                }
            }
    
    
        }
    

    Happy coding! 🙂

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

Sidebar

Related Questions

For the following make file copied below, I am getting the missing separator error.
I copied the timeAgo category for NSDate in XCODE4, but i get the following
I have copied the code from the following example on the Apple Developer site.
I have the following code which i copied from here : /*************************************************************************** * _
I'm at a loss debugging this code. I copied the example from a guide,
I copied the following example from this wikipedia page : struct BasicStruct { int
I have the following code which I'm using to populate form fields. This code
I copied example at http://support.microsoft.com/kb/220595 to the VBA in Excel. My code follows: Dim
Given the following code (copied from the attoparsec library) what does the inline pragma
I've been following this article to display ActiveRecord data in QtRuby. I've copied 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.