How can I change the values of an interface control within a separate task from the main thread?
Example:
private void button1_Click(object sender, EventArgs e)
{
Task task = new Task(Processar);
task.Start();
}
public void Processar()
{
try
{
int i = 0;
this.progressBar1.Maximum = 5000000;
for (i = 0; i < this.progressBar1.Maximum; i++)
{
this.progressBar1.Value = i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
When I run this little bit of code I get this message ...
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
I tried to use delegate, but it did not work.