0

I am facing one issue with Autocomplete, I want to make a autocomplete that will wait for user to enter the complete text.

Let's suppose the user types "T", it will wait 300ms before making the request. If the user types "E" within 300ms it will not hit Any API, then user types "S" and waited 300ms It should hit the API for "TES". But then user types "T" after 10ms it should cancel the previous API request of "TES" and hit the API for "TEST" and so on. I can handle the delay by Task.Delay but don't how to cancel the previous request. Also not sure if Task.Delay is a good solution or not.

    private async void TextChangedEvent(System.Object sender, ItemsChangedEventArgs e)
    {
        var searchText = (sender as AutoComplete).Text;
            await Task.Delay(TimeSpan.FromMilliseconds(3000))
                    .ContinueWith(async task => await GetStatus(searchText),
                    CancellationToken.None,
                    TaskContinuationOptions.OnlyOnRanToCompletion,
                    TaskScheduler.FromCurrentSynchronizationContext());
    }
Mannu
  • 3
  • 2
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 22 '22 at 19:05
  • https://stackoverflow.com/questions/15067865/how-to-use-the-cancellationtoken-property – Jason Sep 22 '22 at 19:39
  • My advice: consider the use of Reactive Extensions to do this kind of things properly. You can use a Throttle to handle this type of scenarios – Rudy Spano Sep 22 '22 at 23:13

0 Answers0