Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there, i want to run a progressbar in a thread. i did this code but it didn't work
could you helpe me please .thanks

C#
 private void button2_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(st);
            th.Start();

           
        }
        void st()
        {

            for (int i = 0; i < 101; i++)
            {
                progressBar1.Value=i;
Thread.Sleep(10);

            }
        }


i get this error
C#
Control.Invoke must be used to interact with controls created on a separate thread.


What I have tried:

private void button2_Click(object sender, EventArgs e)
{
Thread th = new Thread(st);
th.Start();


}
void st()
{

for (int i = 0; i < 100; i++)
{
progressBar1.Value = 100;

}
}
Posted
Updated 13-Jun-16 14:55pm

All you need to do is to marshall the update of the ProgressBar back to the UI thread. This is very easy:
C#
private void button2_Click(object sender, EventArgs e)
{
    Thread th = new Thread(st);
    th.Start();
}
void st()
{
    Function update = (i) => { progressBar1.Value = i; };
    object[] upi = new object[1];
    for (int i = 0; i < 101; i++)
    {
        upi[0] = i;
        progressBar1.Invoke(update, upi);
        Thread.Sleep(10);
    }
}

This will have the started thread running and updating the ProgressBar every 10ms, while the rest of the application runs unimpeded. (You probably want a longer delay to verify that this is running without blocking the UI thread...)

See MSDN: Control.Invoke Method (Delegate, Object[]) (System.Windows.Forms)[^]
 
Share this answer
 
Do this:

C#
private delegate void ProgressHandler();
private void DoProgress()
{
   for (int i = 0; i < 101; i++)
   {
       progressBar1.Value=i;
       Thread.Sleep(10);
 
   }
}


then:

C#
void st()
{
    if (progressBar1.InvokeRequired)
        progressBar1.Invoke(new ProgressHandler(DoProgress));
    else
        DoProgress();
}


However, this kind of ProgressBar is meaningless because ProgressBass has to show some progress in your application.
 
Share this answer
 
v2
Comments
Jammes_Ca 13-Jun-16 5:06am    
big thanks :)
Shahin Khorshidnia 13-Jun-16 5:19am    
Cheers
Jammes_Ca 13-Jun-16 5:25am    
i have an other problem, in my button2_Click program there are several thread.slepp(); and i recognize that the thread.slepp() in my main block the progressbar :( why ?. it's doenst start simultaneously
Shahin Khorshidnia 13-Jun-16 5:40am    
Actually, we need to see the code. Maybe a "Thread.Sleep()" is blocking an important part of a sequential progress. However, we cannot help you unless you provide us with the code.
Jammes_Ca 13-Jun-16 5:53am    
private delegate void ProgressHandler();
private void DoProgress()
{
for (int i = 0; i < 101; i++)
{
progressBar1.Value = i;
Thread.Sleep(1000);

}
}

private void button2_Click(object sender, EventArgs e)
{
Thread th = new Thread(st);
th.Start();
Thread.Sleep(1000);
textBox1.Text = "a";
Thread.Sleep(1000);
textBox1.Text = "b";
Thread.Sleep(1000);
textBox1.Text = "c";
Thread.Sleep(1000);
textBox1.Text = "d";
Thread.Sleep(1000);


}
void st()
{
if (progressBar1.InvokeRequired)
progressBar1.Invoke(new ProgressHandler(DoProgress));
else
DoProgress();
}
the progressbar start after the ending of the code of button2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900