簡體   English   中英

BackgrounderWorker在try-catch塊中停止

[英]BackgrounderWorker stopping in try-catch block

//static int var1, var2, var3;
//static List<string> list;

    public static void MyMethod(Object obj, int times)
    {
        List = doc1.Descendants("Order")
            .Select(d => d.Value)
            .ToList();
        for (int i = 0; i < List.Count; i++)
        {
            try
            {
                var item = (from m in doc1.Elements("list").Elements("Order")
                                where m.Value == List[i]
                                select m);

                item.Remove();

                doc1.Save(path1);
                var1 = doc1.Root.Descendants("Order").Count();


                //DoSomethingHeavy with List[i]

                doc2.Element("list2").Add(new XElement("Order", List[i]));
                doc2.Save(path2);


                 var2= doc2.Root.Descendants("Order").Count();

            }
            catch (Exception)
            {
                doc3.Element("list3").Add(new XElement("Order", List[i]));
                doc3.Save(path3);

                var3 = doc3.Root.Descendants("Order").Count();

            }
        }
    }

這是公共靜態類中的MyMethod。 我想使其在BackgroundWorker下的表單中運行,以便能夠暫停和繼續執行。

我正在使用此類:

public class BackgroundLoading
{
    public BackgroundWorker Bw;
    public delegate void RunFunction();
    public RunFunction thisFunction;



    public BackgroundLoading(RunFunction newFunction)
    {
        thisFunction = newFunction;
        Bw = new BackgroundWorker();
        Bw.DoWork += new DoWorkEventHandler(Bw_DoWork);
        Bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Bw_RunWorkerCompleted);
    }

    public void Start()
    {
        Bw.RunWorkerAsync();
    }

    void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            //an error occured
            MessageBox.Show("an error occured: " + e.Error.Message);
        }
        if (e.Cancelled)
        {
            //an error occured
            MessageBox.Show("Job cancelled");
        }
        else
        {
            MessageBox.Show("Job completed");
        }

    }

    void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        MessageBox.Show(" *");
    }

    void Bw_DoWork(object sender, DoWorkEventArgs e)
    {
        if (thisFunction != null)
            thisFunction();
    }
}

 //class to implement

然后,在myForm中:

private void button1_Click(object sender, EventArgs e)
    {
        BackgroundLoading bl = new BackgroundLoading(MyMethod());
        bl.Start();
    }

我真的是BackgroundWorker的新手,可能我沒有以正確的方式使用它。 單擊StopExecution按鈕時,並未保存我的所有文檔(doc1,doc2,doc3)。 因此,如何避免停止在try-catch塊中?

  1. WorkerSupportsCancellation設置為True,以允許BackgroundWorker支持取消。
  2. 使您的取消按鈕調用CancelAsync()方法。
  3. 通過分成兩種方法來更改您的方法。 一個應獲得項目編號,另一個應逐個項目工作。
  4. 在您的DoWork()方法中,進行檢查以查看是否有取消中的取消 ,如果有,請完成您的處理。

      void Bw_DoWork(object sender, DoWorkEventArgs e) { //in your loop test every time Cancel flag for (int i = 0; i < MyMethodGetItemcount(); i++) { MyMethod(i);//item by item if(yourbackgroundworker.CancellationPending) { e.Cancel = true; return; } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM