1

Currently I am working in WPF application where i am dealing a scenario.

My application has search functionality and depending on the search criteria grid will be populated. To make application responsive we have implemented TPL.

Something like this

Task.Factory.StartNew(() =>
 {
  //Webservice Call
   Dispatcher.Invoke(() =>
   {
     //Data from webservice assigned to Collection binded to Grid
   });
   }
);

I have one problem.

Let's say I have entered search criteria 'A' and hit search, within a second i changed my mind and updated the search criteria with 'B', i hit the search button , again i thought this wrong i have to search something else, i changed the criteria as 'C' and again click the Search. The problem is service response for A in 20 seconds,for B 25 seconds but for C its only 5 seconds.

As per my search i have to show latest search criteria and that would be C but my data is getting updated with B's record. i looked into web but did not able to get any help. Am i missing something. Any help will be appreciated.

anand mishra
  • 125
  • 10

3 Answers3

1

Learn about Cancellation Tokens.

Basically every time you do a search, create a new CancellationTokenSource and keep it in your search class.

Pass the CancellationToken into your async function. After the webcall completes, check to see if the token has been cancelled before updating your results, or just call cancelToken.ThrowIfCancellationRequested(). You may even be able to pass it into your webcall to cancel the call straight away, or use the token's Register() method to explicitly abort the webcall.

If you do a new search, cancel the current token source and create a new one. Any current call will complete without updating.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0

For a very responsive auto search field i'd suggest going with RX Framework instead of trying to roll your own logic, that way you can subscribe to the changed event but also manipulate it (buffer it, keep latest) so that you could easily say Something like "Take key events, ignore them as long as there are new events and less thant 250ms passed, then do a search, drop that search if another search triggered in the meantime, else display it". This is not something you're going to easily compose without RX Framework and nested ifs and cancellations

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
-1

Refer this url will help more as similar things already discuss there and reverted with few answer from experts

Community
  • 1
  • 1