我使用ThreadPool.QueueUserWorkItem来执行通过HTTP执行POST请求的异步任务。
ThreadPool.QueueUserWorkItem(new WaitCallback(UploadPhoto), photoFileName);现在,我想添加取消从UI上传的可能性。
我有两个问题:
ThreadPool适合我的目标吗?发布于 2013-10-03 18:14:38
考虑使用Task.Factory.StartNew在WP7上进行异步工作。您可以使用CancellationTokens强制取消。我就是这样做异步工作的。要实现中断,可以执行以下操作(使用任务):
var task = Task.Factory.StartNew( ( )=>
{
// some operation that will be cancelled
return "some value";
})
.ContinueWith( result =>
{
if(result.Status == TaskStatus.Cancelled) // you have other options here too
{
// handle the cancel
}
else
{
string val = result.Result; // will be "some value";
}
});ContinueWith子句将第一个任务主体完成后发生的另一个方法链接起来(以某种方式)。ContinueWith方法的参数“ContinueWith”是ContinueWith链接到的任务,任务“结果”上有一个名为结果的属性,即前面任务提供的任何返回值。
https://stackoverflow.com/questions/19166184
复制相似问题