How To Start And Stop A Continuously Running Background Worker Using A Button

后端 未结 5 2181
春和景丽
春和景丽 2020-12-01 22:10

Let\'s say I have a background worker like this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
             while(true)
          


        
5条回答
  •  [愿得一人]
    2020-12-01 22:55

    Use the CancelAsync method.

    backgroundworker1.CancelAsync();
    

    In your loop inside the worker thread.

    if (backgroundWorker.CancellationPending) return;
    

    This doesn't happen immediately.

提交回复
热议问题