停止其他项目的后台工作人员
本文关键字:后台 工作人员 项目 其他 | 更新日期: 2023-09-27 17:53:12
我在一个解决方案中有两个项目。一个是form (exe),一个是class (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);
}
}
}
你应该检查一下BackgroundWorker。CancellationRequies在Process方法中,如果检测到取消则退出
for(int i=0; i<newDataCount; i++)
{
if(backgroundWorker.CancellationPending)
{
return;
}