[英]C#.Net - How to cancel a BackgroundWorker pulling data from a WebService
我有以下代码:
void ReferenceManager_DoWork(object sender, DoWorkEventArgs e)
{
try
{
// Get the raw data
byte[] data = this.GetData(IvdSession.Instance.Company.Description, IvdSession.Instance.Company.Password);
// Deserialize the list
List<T> deseriaizedList = null;
using (MemoryStream ms = new MemoryStream(data))
{
deseriaizedList = Serializer.Deserialize<List<T>>(ms);
}
// Convert the list to the Reference Interface
List<IReference> referenceList = new List<IReference>();
foreach (T entity in deseriaizedList)
{
IReference reference = (IReference)entity;
referenceList.Add(reference);
}
e.Result = referenceList;
}
catch (WebException)
{
e.Result = null;
}
}
代码基本上是调用WebService方法的委托。 不幸的是,我使用后台工作程序的主要原因是在加载数据时停止UI冻结。 我有一个弹出的表格,请等待,点击这里取消。
点击取消后,我在后台工作人员上调用CancelAsync。 现在,由于我没有循环,我看不到检查取消的好方法。 我能看到的唯一选择是......
byte[] m_CurrentData;
...在方法之外,在DoWork(..)的启动时启动一个新线程,调用webservice来填充m_CurrentData。 然后,我需要执行循环检查,如果取消或检查m_CurrentData是否不再为null。
有没有更好的取消方式?
实际工作似乎是在this.GetData(...)
方法中完成的,未显示。 我知道它正在调用web服务。 您可能应该调用代理对象上的Abort()
方法来阻止客户端等待响应。 调用CancelAsync()
没有意义,只需确保在RunWorkerCompleted()
正确检查错误。 最简单的方法可能是不捕获_DoWork()
任何异常,但检查Completed()
的Result.Error属性。 无论如何你应该这样做。
只是为了澄清,CancelAsync()方法仅用于在DoWork()内部停止循环。 你没有在那里运行(有意义的)循环,因此需要另一种方法。
更新
我刚刚检查了MSDN for DoWorkEventArgs
并意识到我以前的答案是错误的。 BackgroundWorker
上有CancellationPending
属性,它由对CancelAsync
( 来自MSDN )的调用设置。 因此,您的DoWork
方法可以变为:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;
// Extract the argument.
int arg = (int)e.Argument;
// Start the time-consuming operation.
e.Result = TimeConsumingOperation(bw, arg);
// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
你能用这个吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.