Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new and I am trying to figure something out. I have a simple program which executes a method either when an event happens or when my timer ticks. Lets just say the method takes 5 seconds to complete. During that 5 seconds an event could happen. What would I use/do to make sure my method isn't called while its already running.

Or is this something I would not have to worry about in a simple program, as it wouldn't run until the method is done anyway ?

A little background and example. I am using System.Windows.Forms.Timer, I am not using any threads or anything, and I just have a simple windows form application with a timer, a method to do stuff, and two event handlers.

  public Program(){
    Event.EventHappened += HandleEvent;
  void DoSomething(){
    //Just do something check network, access db write line, something
    //that in this example takes 5 secs for example purposes
  void HandleEvent(){
  void InitMyTimer(){
    // Create Timer, and add Timer tick event
  void TimerTickedEvent(){

I left a few things out of that program, but just was trying to show how simple it is. I was just thinking about making it more efficient so that say in the middle of doing "DoSomething()" an event is triggered, I wouldn't want two "DoSomething()"s method running.

share|improve this question
up vote 4 down vote accepted

As this is using a System.Windows.Forms.Timer instead of System.Threading.Timer, the event will be executed on the UI thread and therefore will not be executed out of sequence.

If you were using threading though, simply creating a private object lockObject = new object(); and locking it in the DoSomething() method, you will similarly ensure that the body of DoSomething does not get executed until it has finished.

share|improve this answer
So does that mean that in the middle of "DoSomething()" being run if an event happens it will just handle the event / call "DoSomething()" again immediately AFTER it has finished ? – RWhiten Feb 3 '14 at 4:12
From the MSDN reference: The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released. – Stephen Feb 3 '14 at 4:19
@RWhite Exactly. – Euphoric Feb 3 '14 at 6:45

If the real intent is to make sure that you won't have multiple worker methods working at the same time the simplest way would be something like this:

bool isBusy = false;
void MyWorkerMethod()
  if(isBusy == true)

    isBusy = true;
    // do stuff
    isBusy = false;

This way every time the timer ticks, it still executes your method but your method will immediately exit if it's in the middle of working with a minimum of fuss. If you use locking you'll get thread contention as things stack up waiting for the lock to free which I'm assuming is not what you want.

If you're super paranoid you could also look into the Interlocked.* APIs to make sure beyond a shadow of a doubt that you never have your worker running more than once but that may be overkill here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.