Why does a System.Timers.Timer survive GC but not System.Threading.Timer?

You can answer this and similar questions with windbg, sos, and !gcroot 0:008> !gcroot -nostacks 0000000002354160 DOMAIN(00000000002FE6A0):HANDLE(Strong):241320:Root:00000000023541a8(System.Thre ading._TimerCallback)-> 00000000023540c8(System.Threading.TimerCallback)-> 0000000002354050(System.Timers.Timer)-> 0000000002354160(System.Threading.Timer) 0:008> In both cases, the native timer has to prevent GC of the callback object (via a GCHandle). The difference is that in the case of System.Timers.Timer the callback references the System.Timers.Timer object (which … Read more

C++ Cross-Platform High-Resolution Timer

Updated answer for an old question: In C++11 you can portably get to the highest resolution timer with: #include <iostream> #include <chrono> #include “chrono_io” int main() { typedef std::chrono::high_resolution_clock Clock; auto t1 = Clock::now(); auto t2 = Clock::now(); std::cout << t2-t1 << ‘\n’; } Example output: 74 nanoseconds “chrono_io” is an extension to ease I/O … Read more

Create a simple 10 second countdown

JavaScript has built in to it a function called setInterval, which takes two arguments – a function, callback and an integer, timeout. When called, setInterval will call the function you give it every timeout milliseconds. For example, if you wanted to make an alert window every 500 milliseconds, you could do something like this. function … Read more

C# – how do you stop a timer?

If you are using System.Timers.Timer stopping is performed by one of the options: //options 1 timer.Enabled = false //option 2 timer.Stop() if you are using System.Threading.Timer, use this method timer.Change(Timeout.Infinite , Timeout.Infinite) if you are using System.Windows.Forms.Timer, use this method timer.Stop();

How do I schedule a task to run at periodic intervals?

Advantage of ScheduledExecutorService over Timer I wish to offer you an alternative to Timer using – ScheduledThreadPoolExecutor, an implementation of the ScheduledExecutorService interface. It has some advantages over the Timer class, according to “Java in Concurrency”: A Timer creates only a single thread for executing timer tasks. If a timer task takes too long to … Read more

Best timing method in C?

I think this should work: #include <time.h> clock_t start = clock(), diff; ProcessIntenseFunction(); diff = clock() – start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf(“Time taken %d seconds %d milliseconds”, msec/1000, msec%1000);

How to access the HttpServerUtility.MapPath method in a Thread or Timer?

It’s possible to use HostingEnvironment.MapPath() instead of HttpContext.Current.Server.MapPath() I haven’t tried it yet in a thread or timer event though. Some (non viable) solutions I considered; The only method I care about on HttpServerUtility is MapPath. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory and build my paths from this. But this will fail if … Read more