I have a textbox in which the user types a search string and then the program makes a search for that string on a background-working-thread.
Right now i'm re-using the same old thread (and wait to make a new search only when the thread is finished/cancelled).
I would be a lot easier if I could just create a new thread each time i want to make a search - because then I would not need to wait for the other thread to be completed before making the search.
The search occurs every time time the text is changed (event textbox.TextChanged) - so that means a lot of new and disposed threads...
Is this a viable strategy or should I continue re-using the same thread (makes room for a lot of potential bugs)?
This is a win-form project in C# 4.0
I have some suggestions:
Use
Task
if you have a naturally asynchronous search system (e.g Entity Framework async API), If not useThreadPool
.Start a timer in your
textbox.TextChanged
event and reset it every time the text changed, if the timer reachs it ends (1 sec) then try searching, this approaches avoid searching fora
,ab
andabc
when you typeabc
fast and meant to search forabc
attach a timestap to every search thread and when the result is ready save it somewhere in you UI, if result of a thread is ready and current result timestamp is bigger than the one being ready then ignore the result.