Make a BackgroundWorker do several operations sequentially without freezing the form

前端 未结 4 880
醉酒成梦
醉酒成梦 2020-12-01 22:51

I have already asked a somwhat similar question here but I now have a follow-up question.

I need to launch the external program several times in a row, but I have se

4条回答
  •  孤街浪徒
    2020-12-01 23:28

    The fact that you have a while (bgwkSVN.IsBusy) { } in your main form thread is why your form stops responding. The background worker is executing it's work on a separate thread but your UI thread is blocked. You should consider starting one RunWorkerAsync() method in the MergerRevisions call and then start the next in the bgwkSVN.RunWorkerCompleted event.

    If you're looking for a nasty quick fix that is the wrong way to do it here it is:

    Change:

    while (bgwkSVN.IsBusy) { }
    

    To:

    while (bgwkSVN.IsBusy) 
    {
        System.Threading.Thread.Sleep(1000); // Make the current (UI/Form) thread sleep for 1 second
        Application.DoEvents();
    }
    

提交回复
热议问题