How to cancel previous Task if new request recieved?

lbrahim

I will try to simplify my situation here to be more clean and concise. So, I am working on a WinRT application where user enters text in a TextBox and in its TextChanged event after 2 seconds have elapsed I need to make a remote request to fetch data based on user text.

Now user enters text and a web request has been initialized but immediately user writes another term. So, I need to cancel the first web request and fire the new one.

Consider the following as my code :

private CancellationTokenSource cts;

public HomePageViewModel()
{
    cts = new CancellationTokenSource();
}

private async void SearchPeopleTextChangedHandler(SearchPeopleTextChangedEventArgs e)
{
    //Cancel previous request before making new one        

    //GetMembers() is using simple HttpClient to PostAsync() and get response
    var members = await _myService.GetMembers(someId, cts.Token);

    //other stuff based on members
}

I know CancellationToken plays a role here but I just cannot figure out how.

Stephen Cleary

あなたはすでにほとんどそれを持っています。基本的な考え方は、シングルCancellationTokenSourceは1回しかキャンセルできないため、操作ごとに新しいものを作成する必要があるということです。

private CancellationTokenSource cts;

private async void SearchPeopleTextChangedHandler(SearchPeopleTextChangedEventArgs e)
{
  // If there's a previous request, cancel it.
  if (cts != null)
    cts.Cancel();

  // Create a CTS for this request.
  cts = new CancellationTokenSource();

  try
  {
    var members = await _myService.GetMembers(someId, cts.Token);

    //other stuff based on members
  }
  catch (OperationCanceledException)
  {
    // This happens if this operation was cancelled.
  }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to cancel a request properly?

分類Dev

How to cancel a Task using CancellationToken?

分類Dev

How do I cancel third party task?

分類Dev

how to know the previous and new positions?

分類Dev

How to use RxJS to cancel previous HTTP Requests in an Angular Service

分類Dev

Cancel Task in async void

分類Dev

How to cancel a request after 5 minutes without a reply

分類Dev

How to cancel a pending request in an inner HTTP observable with RXJS?

分類Dev

Cancel pending $http request

分類Dev

How to get reason for failure of a previous task in vsts CI build

分類Dev

Cancel a loop in a task from outside

分類Dev

how to close all the activities and start a new activity with new task in android

分類Dev

Cancel previous observable returned from another function

分類Dev

how to subtract the next column by the previous column and create a new column after?

分類Dev

Webflux, How to intercept a request and add a new header

分類Dev

How to close bitbucket task in a pull request comment via commit message?

分類Dev

Creating a cancel button... how to completely abort a request in Node/Express.js

分類Dev

Using a CancellationToken to cancel a task without explicitly checking within the task?

分類Dev

How to add a new Activity type to the Task work item in TFS 2018

分類Dev

How to cancel UserNotifications

分類Dev

How to cancel the toast in android?

分類Dev

What is the best way to cancel a socket receive request?

分類Dev

How can I implement the ability to add a new task to an existing list (project) or to a new one

分類Dev

Cancel previous promise.all when onSearch is called again

分類Dev

Cancel jobs submitted previous to a date or with JOBID lower than a given integer

分類Dev

react-native-maps: How to update user location recieved from remote server periodically

分類Dev

How do you attach a new issue to an existing pull request on github?

分類Dev

How to request new facebook permission without logging out

分類Dev

Does Future.cancel(true) remove the task from the queue?

Related 関連記事

  1. 1

    How to cancel a request properly?

  2. 2

    How to cancel a Task using CancellationToken?

  3. 3

    How do I cancel third party task?

  4. 4

    how to know the previous and new positions?

  5. 5

    How to use RxJS to cancel previous HTTP Requests in an Angular Service

  6. 6

    Cancel Task in async void

  7. 7

    How to cancel a request after 5 minutes without a reply

  8. 8

    How to cancel a pending request in an inner HTTP observable with RXJS?

  9. 9

    Cancel pending $http request

  10. 10

    How to get reason for failure of a previous task in vsts CI build

  11. 11

    Cancel a loop in a task from outside

  12. 12

    how to close all the activities and start a new activity with new task in android

  13. 13

    Cancel previous observable returned from another function

  14. 14

    how to subtract the next column by the previous column and create a new column after?

  15. 15

    Webflux, How to intercept a request and add a new header

  16. 16

    How to close bitbucket task in a pull request comment via commit message?

  17. 17

    Creating a cancel button... how to completely abort a request in Node/Express.js

  18. 18

    Using a CancellationToken to cancel a task without explicitly checking within the task?

  19. 19

    How to add a new Activity type to the Task work item in TFS 2018

  20. 20

    How to cancel UserNotifications

  21. 21

    How to cancel the toast in android?

  22. 22

    What is the best way to cancel a socket receive request?

  23. 23

    How can I implement the ability to add a new task to an existing list (project) or to a new one

  24. 24

    Cancel previous promise.all when onSearch is called again

  25. 25

    Cancel jobs submitted previous to a date or with JOBID lower than a given integer

  26. 26

    react-native-maps: How to update user location recieved from remote server periodically

  27. 27

    How do you attach a new issue to an existing pull request on github?

  28. 28

    How to request new facebook permission without logging out

  29. 29

    Does Future.cancel(true) remove the task from the queue?

ホットタグ

アーカイブ