I'm building a progress bar that is controlled by a thread not UI thread, but I always get this error:
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
I tried many ways but it seems doesn't solve the problem. The problem maybe very small but it is still.
Here is code:
private Thread _progressThread;
private ProgressBar pb = new ProgressBar();
private void form_Load(object sender, EventArgs e)
{
pb.Minimum = 0;
pb.Maximum = 300;
Controls.Add(pb);
_progressThread = new Thread(new ThreadStart(this.DoProgress));
_progressThread.IsBackground = true;
_progressThread.Start();
}
private void DoProgress()
{
if (pb.Value == pb.Maximum)
pb.Value = pb.Minimum;
pb.PerformStep();
Thread.Sleep(100);
}
Give me some hints here. Thanks
Cheers.