How can I execute code each 2 minutes without timers ? . I think I can do it with threads , but how ? and is it better to use timer or threads ?
1 Answer
A thread is an unreasonably expensive alternative for a timer, particularly one that ticks that slow. A thread that calls Sleep(2 * 60 * 1000) is not using a megabyte of memory and five operating system handles effectively, the cost of a managed thread.
Use a System.Threading.Timer or System.Timers.Timer instead. The callback/event runs on a threadpool thread so watch out what you do, proper locking is required when you access shared variables. Same considerations as a regular thread.
1 Comment
Trevor
+ 1 for mentioning proper locking