Proper way to implement a never ending task. (Timers vs Task)

I’d use TPL Dataflow for this (since you’re using .NET 4.5 and it uses Task internally). You can easily create an ActionBlock<TInput> which posts items to itself after it’s processed it’s action and waited an appropriate amount of time. First, create a factory that will create your never-ending task: ITargetBlock<DateTimeOffset> CreateNeverEndingTask( Action<DateTimeOffset> action, CancellationToken cancellationToken) … Read more

Is there a Task based replacement for System.Threading.Timer?

It depends on 4.5, but this works. public class PeriodicTask { public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken) { while(!cancellationToken.IsCancellationRequested) { await Task.Delay(period, cancellationToken); if (!cancellationToken.IsCancellationRequested) action(); } } public static Task Run(Action action, TimeSpan period) { return Run(action, period, CancellationToken.None); } } Obviously you could add a generic version that takes … Read more

How to use QueryPerformanceCounter?

#include <windows.h> double PCFreq = 0.0; __int64 CounterStart = 0; void StartCounter() { LARGE_INTEGER li; if(!QueryPerformanceFrequency(&li)) cout << “QueryPerformanceFrequency failed!\n”; PCFreq = double(li.QuadPart)/1000.0; QueryPerformanceCounter(&li); CounterStart = li.QuadPart; } double GetCounter() { LARGE_INTEGER li; QueryPerformanceCounter(&li); return double(li.QuadPart-CounterStart)/PCFreq; } int main() { StartCounter(); Sleep(1000); cout << GetCounter() <<“\n”; return 0; } This program should output a number … Read more

Comparing Timer with DispatcherTimer

Windows.Forms.Timer uses the windows forms message loop to process timer events. It should be used when writing timing events that are being used in Windows Forms applications, and you want the timer to fire on the main UI thread. DispatcherTimer is the WPF timing mechanism. It should be used when you want to handle timing … Read more

C++ obtaining milliseconds time on Linux — clock() doesn’t seem to work properly

#include <sys/time.h> #include <stdio.h> #include <unistd.h> int main() { struct timeval start, end; long mtime, seconds, useconds; gettimeofday(&start, NULL); usleep(2000); gettimeofday(&end, NULL); seconds = end.tv_sec – start.tv_sec; useconds = end.tv_usec – start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf(“Elapsed time: %ld milliseconds\n”, mtime); return 0; }

Javascript: Call a function after specific time period

You can use JavaScript Timing Events to call function after certain interval of time: This shows the alert box every 3 seconds: setInterval(function(){alert(“Hello”)},3000); You can use two method of time event in javascript.i.e. setInterval(): executes a function, over and over again, at specified time intervals setTimeout() : executes a function, once, after waiting a specified … Read more

Is gettimeofday() guaranteed to be of microsecond resolution?

Maybe. But you have bigger problems. gettimeofday() can result in incorrect timings if there are processes on your system that change the timer (ie, ntpd). On a “normal” linux, though, I believe the resolution of gettimeofday() is 10us. It can jump forward and backward and time, consequently, based on the processes running on your system. … Read more

Timer function to provide time in nano seconds using C++

What others have posted about running the function repeatedly in a loop is correct. For Linux (and BSD) you want to use clock_gettime(). #include <sys/time.h> int main() { timespec ts; // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux } For windows you want to use the QueryPerformanceCounter. And here is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)