I'm using the below code to update a progress bar.
But when I debug the program on Visual Studio it breaks on where ever I have used text box controls by giving the below message.
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
I read this issue on the Internet but I can't really figure how to change the code to get around this issue since I'm a total newbie to C# programming.
Appreciate if I can get some help with modifying the code to make the application run properly.
Thanks.
Code:
if (read_success == true && global_variables.csvtext == "not found")
{
this.Unknown_Device.Text = "Writing CSV. Please wait.";
backgroundWorker1.RunWorkerAsync();
}
rivate void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++ )
{
simulateHeavyWork();
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.csvprogressbar.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
writecsv(null, null);
}
private void simulateHeavyWork()
{
Thread.Sleep(40);
}
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
I read this issue on the Internet but I can't really figure how to change the code to get around this issue since I'm a total newbie to C# programming.
Appreciate if I can get some help with modifying the code to make the application run properly.
Thanks.
Comment