I used example calculating Fibonacci, and it works fine.
https://docs.microsoft.com/pl-pl/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0
But if I change Fibonacci calculating for loop the main Window freeze during "doWOrk".
The same situation is when from code below I remove System.Threading.Thread.Sleep(1);
So, Why ? since "doWork" method work in background thread?
(This happens when progress is reported)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var instance = sender as BackgroundWorker;
for (int i = 0; i < 500000; i++)
{
int ik = (int)(i / (float)500000 * 100);
System.Threading.Thread.Sleep(1);
instance.ReportProgress(ik);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
question from:https://stackoverflow.com/questions/66054222/why-c-sharp-backgroundworker-freeze-mainform-thread