Winforms equivalent of javascript setTimeout
Winforms equivalent of javascript setTimeout
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)
Answer by Serguei for Winforms equivalent of javascript setTimeout
You can use a System.Timers.Timer: set AutoReset to false and use Start/Stop methods and create a handler for the Elapsed event.
Answer by Rob Hardy for Winforms equivalent of javascript setTimeout
public void setTimeout(Action TheAction, int Timeout) { Thread t = new Thread( () => { Thread.Sleep(Timeout); TheAction.Invoke(); } ); t.Start(); }
Answer by Robert for Winforms equivalent of javascript setTimeout
I know this is an old question but an alternative solution would be to use Task.Delay(delay).ContinueWith((task) => { /* Code */ });
.
or there is await Task.Delay(delay);
Answer by Ahmet Goktas for Winforms equivalent of javascript setTimeout
public void setTimeout(Action act, int timeout) { Action action = () => { Thread.Sleep(Timeout); act(); }; new Thread(() => Invoke(action)).Start(); }
Answer by Robba for Winforms equivalent of javascript setTimeout
I'd recommend using reactive programming for this. See https://github.com/Reactive-Extensions/Rx.NET for the Reactive Extensions for .NET and http://reactivex.io/ for the general information about Reactive programming.
I'm afraid I'm only familiar with the JavaScript reactive library, so I can't give you a C-Sharp example, but in JavaScript it'd work something like this:
Rx.Observable.fromEvent(..eventdetails..) .debounceTime(300) .distinctUntilChanged() .subscribe(eventHandler);
Using a setup like this you can chain operators to map and merge all kinds of events from a source to a subscriber. The simple example above reacts to an event, a keyUp for instance, and waits until there is no new keyUp for 300 ms and then calls the eventHandler, but only if the new value (after 300ms) is different from the last emitted value.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment