Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
337 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...