【发布时间】:2014-03-08 06:36:51
【问题描述】:
如何在 C# 中取消线程,当我调用 Web 服务时,例如:
这段代码如下:
private BackgroundWorker doWorkAnuncios;
public myclass()
{
doWorkAnuncios = new BackgroundWorker();
doWorkAnuncios.WorkerSupportsCancellation = true;
}
public void someMethod()
{
doWorkAnuncios.RunWorkerCompleted += new RunWorkerCompletedEventHandler(doWorkAnuncios_RunWorkerCompleted);
doWorkAnuncios.DoWork += new DoWorkEventHandler(doWorkAnuncios_DoWork);
doWorkAnuncios.RunWorkerAsync();
}
private void doWorkAnuncios_DoWork(object sender, DoWorkEventArgs e)//Call the web service
{
_dataCustomer = new Customers(); //this object sends the customer number
_lstCustomers = _dataCustomer.GetDetailsCustomers(CustomerNumber);//Send a customer number
//In this part check the CancellationPending, but when it finish the process in the web service,
//if i decide to cancel the process, it do not cancell the request.**
if (doWorkAnuncios.CancellationPending)//try to cancel the background
{
e.Cancel = true;
return;
}
}
我想用方法、函数或事件点击取消线程,你能帮帮我吗?
我不开发 Web 服务,我只使用方法。我在 C# 中使用 3.5 框架。
【问题讨论】: