private void button2_Click(object sender, EventArgs e)
{
this.button2.Enabled = false;
// Worker Thread.
new Thread(delegate()
{
for (int i = 0; i < 100; Interlocked.Increment(ref i))
{
// Update UI on UI thread using blocking Invoke.
this.Invoke((ThreadStart)delegate()
{
this.progressBar1.Value = i;
this.textBox1.Text = "Percent Complete: " + i + "%";
});
Thread.Sleep(5); // Just add a pause to sim work.
}
// Call any completion code here. All Invokes above must have completed as Invoke is blocking.
this.Invoke((ThreadStart)delegate()
{
this.textBox1.Text = "Percent Complete: 100%";
this.button2.Enabled = true;
});
}).Start();
}