This question already has an answer here:
- Reporting progress from Async Task 1 answer
I have a method in another dll that gets a lot of data (lopping 22k rows), and I need to create a progress bar for the user.
If I do not call this method asynchronously it hangs the application. But when I call it asynchronously I get the error progressBar1
Control accessed from a thread other than the thread it was created on
private void btnSend_Click(object sender, EventArgs e)
{
string filePath = tbPath.Text;
ETLBusiness etlBusiness = new ETLBusiness(filePath);
Task.Run(() => etlBusiness.LoadData(progressBar1));
}
In that case, backgroundWorker1.RunWorkerAsync
is not asynchronous, it is stopping my application in looping
private void btnSend_Click(object sender, EventArgs e)
{
string filePath = tbPath.Text;
ETLBusiness etlBusiness = new ETLBusiness(filePath);
Func<ProgressBar,int> func = etlBusiness.LoadData;
this.backgroundWorker1.RunWorkerAsync(func(progressBar1));
}
how fixed it?
Your problem is that your control exists on thread A but you are trying to access it via thread B. This is a 'no-no'. Try the following code
Where 'ProgressBar1ObjectType' is the Type of progressBar1
Here is another option that may work better: