I need run one method in thread. But the thread has to run after 5 seconds after inactivity. Inactivity means stop writing to textbox. Between changes of text in textbox(when time before changes is shorter then 5 seconds) I need reset delay to start thread again to 5 seconds. If user stops writing to textbox for 5 seconds thread starts and do some work. At least when user writes to textbox while thread works delay no change, only let thread to finish.
Asked
Active
Viewed 15 times
0
-
In other words, you want to do something in the background only after 5 seconds of inactivity. That's not a dynamic delay either. What you ask makes sense only for desktop applications too. Both Windows Forms and WPF have Idle events that allow you to run code when the app is idle. – Panagiotis Kanavos Jun 04 '20 at 12:39
-
@PanagiotisKanavos They're looking to do something when a textbox has been unchanged for 5 seconds, not when the entire application has had no events for 5 seconds. You only need a change event handler on the textbox for that, which pretty much any UI platform will provide some means of accomplishing. – Servy Jun 04 '20 at 12:42
-
What are you *actually* trying to do? Combo boxes for example have an autocomplete delay so they don't have to perform a search until the user stops typing. That's a one-of search though. What does your thread try to do? Do you really need to pause it? – Panagiotis Kanavos Jun 04 '20 at 12:42
-
@Servy I know. But the question right now is too vague and a lot depends on the actual job the thread performs. There may not even be a need for a textbook event if the OP wants to implement autocomplete. Or something like Rx's throttling is better. Or a thread with cooperative pause triggered by a timer. – Panagiotis Kanavos Jun 04 '20 at 12:45
-
Yes, it is autocomplete in wpf application. The thread gets data from database to fill listbox. I need the pause, it is request :) – Pimpy Jun 04 '20 at 12:45
-
In that case you don't need a thread at all. Some controls already support autocomplete delays. You'll find many duplicates that show how to implement autocomplete delays in SO. You haven't explained which stack or control you use though - Winforms? WPF ? – Panagiotis Kanavos Jun 04 '20 at 12:50
-
The simplest solution, described in many SO questions, is to use a timer that performs the lookup after 5 seconds and reset it in the KeyUp event. – Panagiotis Kanavos Jun 04 '20 at 12:52
-
I want to avoid freezing application when get data from database. – Pimpy Jun 04 '20 at 13:00