1

我有这个非异步任务>刚刚要求:取消taskcompletionsource从一个API调用一个void方法与超时xamarin形式

TaskCompletionSource<ObservableCollection<ItemDto>> tcs = new TaskCompletionSource<ObservableCollection<ItemDto>>(); 

     ObservableCollection<ItemDto> results = new ObservableCollection<ItemDto>(); 

     try 
     { 
      BasicHttpBinding binding = new BasicHttpBinding(); 
      binding.OpenTimeout = new TimeSpan(0, 0, 30); 
      binding.CloseTimeout = new TimeSpan(0, 0, 30); 
      binding.SendTimeout = new TimeSpan(0, 0, 30); 
      binding.ReceiveTimeout = new TimeSpan(0, 0, 30); 

      MobileClient clientMobile = new MobileClient(binding, new EndpointAddress(_endpointUrl)); 

      clientMobile.FindItemsCompleted += (object sender, FindItemsCompletedEventArgs e) => 
      { 
       if (e.Error != null) 
       { 
        _error = e.Error.Message; 
        tcs.TrySetException(e.Error); 
       } 
       else if (e.Cancelled) 
       { 
        _error = "Cancelled"; 
        tcs.TrySetCanceled(); 
       } 

       if (string.IsNullOrWhiteSpace(_error) && e.Result.Count() > 0) 
       { 
        results = SetItemList(e.Result); 

        tcs.TrySetResult(results); 
       } 
       clientMobile.CloseAsync(); 
      }; 
      clientMobile.FindItemsAsync(SetSearchParam(searchString, 100)); 
     } 
     catch (Exception) 
     { 
      results = new ObservableCollection<ItemDto>(); 
      tcs.TrySetResult(results); 
     } 
     return tcs.Task; 

是的,我知道,没有什么特别的,它是这个

只是

clientMobile.FindItemsAsync(SetSearchParam(searchString的,100))

是空隙的方法,后者又调用另一空隙方法的调用,其设置一些参数,然后调用一个异步方法,它自己调用异步方法执行异步操作以返回项目列表。

问题是,我无法控制任何超出上述任务范围的任何内容,因为我刚刚解释的所有内容都是API的一部分,我不允许触及其中的任何部分,而且我不能触及其中的任何部分评论,关于它的工作方式,因为政策是为了让我的工作适应它... -_-

所以,为了做到这一点,我必须尽快杀掉对FindItemsAsync的调用,总共1分钟已经过去了......我试着将上面的时间段设置为每分钟一分钟(第一次工作,现在已经做了一些更改并且没有开始),我试图缩短到一半时间,并且没有去...

下面是调用此任务的代码:

public void LoadItemList(string searchString) 
    { 
     _itemList = new ObservableCollection<ItemDto>(); 

     // Calls the Task LoadList. 
     var result = LoadList(searchString).Result; 

     if (result != null && result != new ObservableCollection<ItemDto>()) 
     { 
      _itemList = result; 
     } 
     else 
     { 
      _isTaskCompleted = false; 
     } 

     _isListEmpty = (_itemList != new ObservableCollection<ItemDto>()) ? false : true; 
    } 

以下是调用该任务的调用者的代码...(什么乱七八糟的-_-):

void Init(string searchString = "") 
    { 
     Device.BeginInvokeOnMainThread(async() => 
     { 
      if (!LoadingStackLayout.IsVisible && !LoadingActivityIndicator.IsRunning) 
      { 
       ToggleDisplayLoadingListView(true); 
      } 

      await Task.Run(() => _listVM.LoadItemList(searchString)); 

      ToggleDisplayLoadingListView(); 

      if (!string.IsNullOrWhiteSpace(_listVM.Error)) 
      { 
       await DisplayAlert("Error", _listVM.Error, "OK"); 
      } 
      else if (_listVM.AdList != null && !_listVM.IsListEmpty) 
      { 
       ItemListView.IsVisible = true; 

       ItemListView.ItemsSource = _listVM.ItemList; 
      } 
      else if (!_listVM.IsTaskCompleted || _listVM.IsListEmpty) 
      { 
       await DisplayAlert("", "At the moment it is not possible to show results for your search.", "OK"); 
      } 
      else if (_listVM.ItemList.Count == 0) 
      { 
       await DisplayAlert("", "At the moment there are no results for your search.", "OK"); 
      } 
     }); 
    } 

目前我想实现MVVM拱...

真的,非常感谢你对你在这个问题上的帮助,这是伟大的,我真的很为这一切的不便表示歉意......

编辑

对不起,因为我没有清楚地解释我的目标;它是:我需要获取一个访问API的项目列表,它只是通过一个void方法FindItemsAsync与我进行通信。我有60秒钟来获取所有这些物品。如果出现问题或超时,我必须取消流程并通知用户出现问题。

这没有发生。它永远不会取消。要么让我的项目,或永远保持加载,尽管我最难的尝试...我是新任务和大部分这些东西,因此我的问题不断...

回答

2

当您的取消令牌过期时,您可以调用CloseAsync 。

//Creates an object which cancels itself after 5000 ms 
var cancel = new CancellationTokenSource(5000); 

//Give "cancel.Token" as a submethod parameter 
public void SomeMethod(CancellationToken cancelToken) 
{ 
    ... 

    //Then use the CancellationToken to force close the connection once you created it 
    cancelToken.Register(()=> clientMobile.CloseAsync()); 
} 

它会减少连接。

+0

看到问题是我没有取消令牌,甚至不知道如何使用一个...我拥有的只是压力,而对某些东西的要求永远不够...你会介意多解释一下关于我如何使用这个取消标记,或者如果这是什么从这个等式中缺少,请? –

+0

您使用了TaskCompletionSource,因此您应该了解CancellationToken。 – Softlion

+0

对不起,我知道它的存在,但不知道如何使用它,并且主要是从MSDN示例中复制这些代码,因为那时我试图等待API的结果,并且因为我没有使用任何异步/等待的东西,我在收到结果之前完成了被调用的方法,因此实现了我找到的这个解决方案... –

相关问题