0

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
  • Does the code have minimal interaction with the UI? Commented Mar 2, 2013 at 16:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+ 1 for mentioning proper locking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.