Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete: Asynchronous population with WCF using threads

Tags:

c#

.net

wcf

I have an "autocomplete" textbox that will invoke a WCF method each time a key is pressed. The WCF server, in turn, will run an SQL query, return the first 15 results and send them. However, this results in a noticeable latency when typing in the box.

What I'm about to do instead is this:

  • Create a new thread when a text_changed event is fired, make that thread wait 1000ms using Stopwatch.ElapsedMilliseconds. During this waiting time, the thread can be stopped permanently.
  • If it was not stopped, the thread will send the request to the server (and repopulate the auto complete box).
  • As soon as a new "text_changed" event is fired, I will stop the current thread and start a new one.

Is there a better approach or is this the way to go?

like image 811
David Avatar asked Mar 28 '26 16:03

David


1 Answers

So basically wait for 1 second for the user to stop typing before requesting results.

That's a good solution for conserving server resources, but you are actually adding latency by making the user wait for a minimum of 1000ms.

My guess is that your original issue was that this is a winforms app and the request you made was synchronous by default. As a result, the textbox wasn't accepting user input while the app was waiting for a response. Just making the call asynchronous should solve that issue without making the typing slower.

like image 76
LouD Avatar answered Mar 31 '26 05:03

LouD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!