我在一个解决方案中有两个项目。一个是形式(exe),一个是类(dll)。
我有一个这样的背景工作者(在表单项目中):
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,过程也会一直持续到完成。 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 :(得分:0)
您应该检查Process方法中的BackgroundWorker.CancellationRequies并在检测到取消时退出
for(int i=0; i<newDataCount; i++)
{
if(backgroundWorker.CancellationPending)
{
return;
}