Winforms javascript equivalent setTimeout equivalent

Is there a simple solution / idea / strategy for creating an equivalent setTimeout function in a WinForms application. I am primarily a web developer, but not sure how to do this in a WinForms application. Basically, I have a text box, and after every keystroke, I want to run a task to populate a list (for example, an auto-complete type thingy), but I want to be able to cancel (for example, clearTimeout) if the user continues to enter characters .. .

My only assumption is that you can use BackGroundWorker and compose it first, and while he sleeps, you can cancel it if the user stops entering keys and the waiting period ends, then he starts and starts the task, etc.

(I don't care what the C # or Vb.Net example is)

+10
javascript winforms settimeout
source share
9 answers

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

+13
source share

I know this is an old question, but an alternative solution would be to use Task.Delay(delay).ContinueWith((task) => { /* Code */ }); .

Thread.Sleep vs Task.Delay?

or there is await Task.Delay(delay);

https://social.msdn.microsoft.com/Forums/vstudio/en-US/345f0402-3af0-4f96-a501-073674883ba3/building-an-async-settimeout-function?forum=csharpgeneral

+30
source share
  public void setTimeout(Action TheAction, int Timeout) { Thread t = new Thread( () => { Thread.Sleep(Timeout); TheAction.Invoke(); } ); t.Start(); } 
+10
source share

I can suggest the following

 internal class Timeout : System.Timers.Timer { public Timeout (Action action, double delay) { this.AutoReset = false; this.Interval = delay; this.Elapsed += (sender, args) => action(); this.Start(); } } // Example var timeout = new Timeout(() => { Console.WriteLine("init 1"); }, 500); timeout.Stop(); 
+3
source share

Timer Implementation:

 public void SetTimeout(Action action, int timeout) { var timer = new System.Windows.Forms.Timer(); timer.Interval = timeout; timer.Tick += delegate (object sender, EventArgs args) { action(); timer.Stop(); }; timer.Start(); } 
+3
source share
  public void setTimeout(Action act, int timeout) { Action action = () => { Thread.Sleep(Timeout); act(); }; new Thread(() => Invoke(action)).Start(); } 
0
source share

I would recommend using reactive programming for this. See https://github.com/Reactive-Extensions/Rx.NET for reactive extensions for .NET and http://reactivex.io/ for general information on active programming.

I'm afraid that I am familiar with the reactive JavaScript library, so I cannot give you an example of C-Sharp, but in JavaScript it will work something like this:

 Rx.Observable.fromEvent(..eventdetails..) .debounceTime(300) .distinctUntilChanged() .subscribe(eventHandler); 

Using this setting, you can bind operators to display and combine all kinds of events from source to subscriber. The simple example above refers to an event, such as keyUp, and waits until there is a new keyUp in 300 ms, then calls eventHandler, but only if the new value (after 300 ms) differs from the last emitted value.

0
source share

This is my way, use C # 7.0 syntax. Some js differ, when the timeout action is executed then it may not be clear.

 internal static class JsStyleTimeout { private static readonly ConcurrentDictionary<int, Thread> InnerDic; private static int _handle; static JsStyleTimeout() { InnerDic = new ConcurrentDictionary<int, Thread>(); } public static int Set(Action action, int delayMs) { var handle = Interlocked.Increment(ref _handle); var thread = new Thread(new ThreadStart(delegate { Thread.Sleep(delayMs); InnerDic.TryRemove(handle, out var _); Task.Factory.StartNew(action); })); InnerDic.TryAdd(handle, thread); thread.Start(); return handle; } public static void Clear(int handle) { if (InnerDic.TryRemove(handle, out var thread)) thread.Abort(); } } 
0
source share

You can also use:

 Delay.Do(3000 /*in ms*/, () => { /* Do somthing */ }); 

Where is Delay.Do located:

 using System; using System.Timers; public class Delay { public static void Do(int after, Action action) { if (after <= 0 || action == null) return; var timer = new Timer { Interval = after, Enabled = false }; timer.Elapsed += (sender, e) => { timer.Stop(); action.Invoke(); timer.Dispose(); GC.SuppressFinalize(timer); }; timer.Start(); } } 

Note : when updating a control in a user interface thread, use Control.Invoke :

 Delay.Do(2000, () => { lblOk.Invoke((MethodInvoker)(() => { lblOk.Visible = false; })); }); 
0
source share

All Articles