Xamarin.forms CancellationToken源在xamarin表单中不工作
我有5个任务一起运行Xamarin.forms CancellationToken源在xamarin表单中不工作,xamarin.forms,cancellationtokensource,cancellation-token,Xamarin.forms,Cancellationtokensource,Cancellation Token,我有5个任务一起运行 var tasks = new ConcurrentBag<Task>(); cancellationTokenSource = new CancellationTokenSource(); var token = cancellationTokenSource.Token; Task<InvServiceModel> t1 = GetInventoryLine(cancellatio
var tasks = new ConcurrentBag<Task>();
cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
Task<InvServiceModel> t1 = GetInventoryLine(cancellationTokenSource.Token);
tasks.Add(t1);
Task<ProductServiceModel> t2 = GetProductData(cancellationTokenSource.Token);
tasks.Add(t2);
Task<List<InvLineCost>> t3 = GetInvLineCostData(cancellationTokenSource.Token);
tasks.Add(t3);
Task<MiscDataMobileServiceModel> t4 = GetMiscellaniousMobileData(cancellationTokenSource.Token);
tasks.Add(t4);
Task<RecipeHierarchyModel> t5 = GetRecipeHierarhyData(cancellationTokenSource.Token);
tasks.Add(t5);
Task<UnknownInvStorage> t6 = GetUnknownStorageLocationData(cancellationTokenSource.Token);
tasks.Add(t6);
try
{
await Task.WhenAll(tasks.ToArray());
}
catch (AggregateException ae)
{
if (ae.InnerExceptions.Any(e => e is TaskCanceledException))
Console.WriteLine("Task cancelled exception detected");
else
throw;
}
catch (OperationCanceledException ae)
{
Console.WriteLine("Task cancelled exception detected {0}", ae);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
cancellationTokenSource.Dispose();
}
如何取消所有任务?似乎有一些问题
cancellationToken.ThrowIfCancellationRequested();
未在单个任务方法中调用
服务调用方法示例如下:
public async Task<InvServiceModel> GetInventoryLineItems(string Restaurantid, string
FromDate, CancellationToken cancellationToken)
{
InvServiceModel invservicemodel = null;
HttpResponseMessage responseMessage = null;
try
{
cancellationToken.ThrowIfCancellationRequested();
var api = RestService.For<IInventoryLineItemsApi>(_httpClient);
responseMessage = await api.GetInventoryLineItems(Restaurantid, FromDate, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
//
}
else
{
//
throw serviceException;
}
}
catch (TaskCanceledException ex)
{
Console.WriteLine("TaskCanceledException GetInventoryLineItems");
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return invservicemodel;
}
public异步任务GetInventoryLineItems(字符串Restaurantid,字符串
FromDate,CancellationToken CancellationToken)
{
InvServiceModel InvServiceModel=null;
HttpResponseMessage responseMessage=null;
尝试
{
cancellationToken.ThrowIfCancellationRequested();
var api=RestService.For(_httpClient);
responseMessage=await api.GetInventoryLineItems(Restaurantid、FromDate、cancellationToken)。ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if(responseMessage.StatusCode==System.Net.HttpStatusCode.OK)
{
//
}
其他的
{
//
抛出serviceException;
}
}
捕获(TaskCanceledException ex)
{
控制台.WriteLine(“TaskCanceledException GetInventoryLineItems”);
掷骰子;
}
捕获(例外情况除外)
{
掷骰子;
}
返回模型;
}
您需要保留对它的引用,以便下次调用处理程序时可以取消上一次搜索。检查。您需要保留对它的引用,以便下次调用处理程序时可以取消上一次搜索。检查。
public async Task<InvServiceModel> GetInventoryLineItems(string Restaurantid, string
FromDate, CancellationToken cancellationToken)
{
InvServiceModel invservicemodel = null;
HttpResponseMessage responseMessage = null;
try
{
cancellationToken.ThrowIfCancellationRequested();
var api = RestService.For<IInventoryLineItemsApi>(_httpClient);
responseMessage = await api.GetInventoryLineItems(Restaurantid, FromDate, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
//
}
else
{
//
throw serviceException;
}
}
catch (TaskCanceledException ex)
{
Console.WriteLine("TaskCanceledException GetInventoryLineItems");
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return invservicemodel;
}