Difference between DispatchSourceTimer, Timer and asyncAfter?

Timer is a Swift bridge of NSTimer, which goes back to NeXTSTEP, long, long before Grand Central Dispatch (GCD) and things like DispatchSourceTimer, which didn’t come along until 10.6 (in the form of dispatch_source_set_timer) and dispatchAfter (in the form of dispatch_after). NSTimer is based on the run loop, which was the primary way that concurrency … Read more

What is the equivalent of javascript setTimeout in Java?

Asynchronous implementation with JDK 1.8: public static void setTimeout(Runnable runnable, int delay){ new Thread(() -> { try { Thread.sleep(delay); runnable.run(); } catch (Exception e){ System.err.println(e); } }).start(); } To call with lambda expression: setTimeout(() -> System.out.println(“test”), 1000); Or with method reference: setTimeout(anInstance::aMethod, 1000); To deal with the current running thread only use a synchronous version: … Read more

Call jQuery Ajax Request Each X Minutes

You can use the built-in javascript setInterval. var ajax_call = function() { //your jQuery ajax code }; var interval = 1000 * 60 * X; // where X is your every X minutes setInterval(ajax_call, interval); or if you are the more terse type … setInterval(function() { //your jQuery ajax code }, 1000 * 60 * … Read more

Compare using Thread.Sleep and Timer for delayed execution

One difference is that System.Threading.Timer dispatches the callback on a thread pool thread, rather than creating a new thread every time. If you need this to happen more than once during the life of your application, this will save the overhead of creating and destroying a bunch of threads (a process which is very resource … Read more

Wait some seconds without blocking UI execution

I think what you are after is Task.Delay. This doesn’t block the thread like Sleep does and it means you can do this using a single thread using the async programming model. async Task PutTaskDelay() { await Task.Delay(5000); } private async void btnTaskDelay_Click(object sender, EventArgs e) { await PutTaskDelay(); MessageBox.Show(“I am back”); }

How to fire timer.Elapsed event immediately

Just call the Timer_Tick method yourself. If you don’t want to deal with the Tick callback method’s parameters, then just put the code that was in your Timer_Tick into another method, and call that from the Timer_Tick and from just after the Timer.Start() call As pointed out by @Yahia, you could also use the System.Threading.Timer … Read more

Why are .NET timers limited to 15 ms resolution?

Perhaps the document linked here explains it a bit. It’s kinda dry so I only browsed it quickly 🙂 Quoting the intro: The system timer resolution determines how frequently Windows performs two main actions: Update the timer tick count if a full tick has elapsed. Check whether a scheduled timer object has expired. A timer … Read more