Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  10] [ 2]  / answers: 1 / hits: 40518  / 14 Years ago, thu, december 2, 2010, 12:00:00

Is there a simple solution/idea/strategy to create a setTimeout equivalent function in a WinForms app. I'm primarily a web developer but am not sure how I'd go about this in a WinForms App. Basically, I have a textbox, and after each keystroke I want to run a task to populate a list (like an auto-complete type thingy) but want to be able to cancel (e.g. clearTimeout) if the user keeps entering characters...



My only guess is to perhaps use a BackGroundWorker and make it sleep initially, and while it is sleeping, it could be cancelled, if the user stops entering keys and the sleep period ends, it then goes and runs the task etc



(i don't care if an example is C# or Vb.Net)


More From » .net

 Answers
410

You can use a System.Timers.Timer: set AutoReset to false and use Start/Stop methods and create a handler for the Elapsed event.


Here's an example implementation in vb.net:


  Public Sub SetTimeout(act As Action, timeout as Integer)
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer(1)
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, Sub () act
aTimer.AutoReset = False
aTimer.Enabled = True
End Sub

[#94768] Tuesday, November 30, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrell

Total Points: 109
Total Questions: 113
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
darrell questions
Mon, May 9, 22, 00:00, 2 Years ago
Sat, Mar 19, 22, 00:00, 2 Years ago
Sat, Oct 10, 20, 00:00, 4 Years ago
;