All you need to do is to marshall the update of the
ProgressBar
back to the UI thread. This is very easy:
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)[
^]