2014-03-25 79 views
0

我在一個解決方案中有兩個項目。一個是form(exe),另一個是class(dll)。停止其他項目的後臺工作

我有一個BackgroundWorker(在形式上項目)是這樣的:

private void CalculateBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) 
{   
     try 
     { 
      BackgroundWorker worker = sender as BackgroundWorker; 

      if (worker.CancellationPending == true) 
      { 
       e.Cancel = true; 
      } 
      else 
      { 
       Grav_Fluid.Rule.Processing_Controller.SpectralAnalysisFormController spectralAnalysisFormController = new Grav_Fluid.Rule.Processing_Controller.SpectralAnalysisFormController(); 
       outputDataTable = spectralAnalysisFormController.Proccess(inputDataTable, double.Parse(DistanceIntervalTextBox.Text), worker); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

這是類項目發生什麼(DLL):

public DataTable Proccess(DataTable inputDataTable, double ITV, System.ComponentModel.BackgroundWorker backgroundWorker) 
    { 
     try 
     { 
      int RowCount = inputDataTable.Rows.Count; 

      int orde = 7; 
      int dataCount = inputDataTable.Rows.Count; 
      double[] coef = new double[orde + 3]; 
      double[] distance = new double[dataCount]; 
      ... 
      ... 
      for(int i=0; i<newDataCount; i++) 
      { 
       ... 
       backgroundWorker.ReportProgress(Convert.ToInt32(i * 80/newDataCount)); 
      } 
      return dT; 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 
    } 

我的問題是,如何好辦法取消我的背景工作?我只是想移動這個代碼:

if (worker.CancellationPending == true) 
{ 
    e.Cancel = true; 
} 

到類項目,所以我可以立即停止該過程。但我做不到。誰能幫我?

================================

更新: 從其他的取消本身功能:

private void buttonCancel_Click(object sender, EventArgs e) 
    { 
     if (CalculateBackgroundWorker.WorkerSupportsCancellation == true) 
     { 
      CalculateBackgroundWorker.CancelAsync(); 
      LoaderForm.Close(); 
     } 
    } 

    private void CalculateButton_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      ClearOutputDataTable(); 
      if (CalculateBackgroundWorker.IsBusy != true) 
      { 
       LoaderForm = new General.LoaderForm(); 
       LoaderForm.Canceled += new EventHandler<EventArgs>(buttonCancel_Click); 
       LoaderForm.Show(); 
       LoaderForm.TopMost = true; 
       CalculateBackgroundWorker.RunWorkerAsync(); 
      } 
     } 
     catch (Exception ex) 
     { 
       Messagebox.Show(ex.Message); 
     } 
    } 

我更新我的問題。所以我加載了顯示進度條的表單。它會在計算過程中出現。此表單具有取消按鈕,該按鈕將執行以取消該過程。

這是loaderform背後代碼:

public partial class LoaderForm : Form 
{ 
    public LoaderForm() 
    { 
     InitializeComponent(); 
    } 

    public string Message 
    { 
     set { PercentageLabel.Text = value; } 
    } 

    public int ProgressValue 
    { 
     set { LoadProgressBar.Value = value; } 
    } 

    public event EventHandler<EventArgs> Canceled; 

    private void CancelButton_Click(object sender, EventArgs e) 
    { 
     EventHandler<EventArgs> ea = Canceled; 
     if (ea != null) 
      ea(this, e); 
    } 
} 

更新2: 連我叫cancellAsync中,proccess不斷繼續下去,直到完成。在BackgroundWorker_DoWork的代碼,只有在這種情況下(例如)工作:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 

     for (int i = 1; i <= 10; i++) 
     { 
      if (worker.CancellationPending == true) 
      { 
       e.Cancel = true; 
       break; 
      } 
      else 
      { 
       // Perform a time consuming operation and report progress. 
       worker.ReportProgress(i * 10); 
       System.Threading.Thread.Sleep(500); 
      } 
     } 
    } 

回答

0

你應該在你的加工方法和出口檢查BackgroundWorker.CancellationRequies如果取消檢測

for(int i=0; i<newDataCount; i++) 
{ 
    if(backgroundWorker.CancellationPending) 
    { 
    return; 
    } 
+0

更新我的問題。所以我加載了顯示進度條的表單。它會在計算過程中出現。此表單具有取消按鈕,該按鈕將執行以取消該過程。 – ehmind

+0

我不理解你,你的代碼將調用CancellAsync並將CancellationPending設置爲true,所以在下一次當你檢測到它時,你將停止處理 - 一切正確! –

+0

即使我打電話給cancellAsync,這個過程仍會繼續直到完成。 BackgroundWorker_DoWork中的代碼僅適用於以下情況:for(int i = 1; i <= 10; i ++) if(worker.CancellationPending == true) e.Cancel = true; 休息; } else { //執行耗時的操作並報告進度。 worker.ReportProgress(i * 10); System.Threading.Thread.Sleep(500); } }' – ehmind

相關問題