# Custom timer code needs reviewing for thread safety

I've created a custom timer to satisfy the following requirements:

• Millisecond accuracy
• I want the tick event handler to only be called once the current tick handler has completed (much like the winforms timer)
• I want exceptions on the main UI thread not to be swallowed up by the thread timer so this requires Invoke/Send instead of BeginInvoke/Post

Note: I call timeBeginPeriod(1)/ timeEndPeriod(1) in order to achieve the millisecond accuracy.

I've put in a check if the timer is deleted to make sure that the deletion is complete before the timer can be created again. Does that look ok?

For the IsRunning property, is it possible that racing conditions could occur?

...or is the code a disaster waiting to happen and I should try (again) to get CreateTimerQueueTimer to work?

The code has been tested with a small interval of 1 millisecond and a large interval of 300 milliseconds with frequent start/stops, and I've had no problems so far.

ETA: I've found a problem with it. If the timer is running at an interval of 1 millisecond, and I call, say, Change(300), it locks up @ while (this.DeleteRequest). This must be because the TimerLoop is in the this.CallbackDelegate.Invoke(null) call.

public class MyTimer : IDisposable
{

private bool DeleteRequest;

{
this.CallbackDelegate = callBack;
}

public void Create(int interval)
{
while (this.DeleteRequest) {
}

throw new Exception("");
}

// Make sure the thread is automatically killed when the app is closed.

}

public void Change(int interval)
{
// A lock required here?
if (!this.IsRunning()) {
throw new Exception("");
}
this.Delete();
this.Create(interval);
}

public void Delete()
{
this.DeleteRequest = true;
}

public bool IsRunning()
{
}

private void TimerLoop(object args)
{
int interval = (int)args;
Stopwatch sw = new Stopwatch();
sw.Start();

do {
if (this.DeleteRequest) {
this.DeleteRequest = false;
return;
}

long t1 = sw.ElapsedMilliseconds;

// I want to wait until the operation completes, so I use Invoke.
this.CallbackDelegate.Invoke(null);

if (this.DeleteRequest) {
this.DeleteRequest = false;
return;
}

long t2 = sw.ElapsedMilliseconds;

int temp = Convert.ToInt32(Math.Max(interval - (t2 - t1), 0));
sw.Reset();
if (temp > 0) {
}

sw.Start();
} while (true);

}

// dispose calls this.Delete();

}

• If you found a solution, please post an answer with it and accept it instead of editing your post. That way future visitors can easily see what your solution was. Sep 12, 2011 at 17:45