How to trigger a timer tick programmatically?

You could set the interval to 1 (0 probably means infinite timeout) and set it to 10k when it ticks for the first time.

The timer will tick "very soon" (depending what type of Timer you are using, see this) but the execution will not continue with the click handler as in your solution.

(I suppose you knew about the solution from Bevan).


There are at least 4 different "Timers" in .NET. Using System.Threading you can get one that specifically allows you to set the initial delay.

var Timer = new Timer(Timer_Elapsed, null, 0, 10000);

There are benefits to using the different timers and here is a good article discussing them all.


The only thing I'd do differently is to move the actual Tick functionality into a separate method, so that you don't have to call the event directly.

myTimer.Start();
ProcessTick();

private void MyTimer_Tick(...)
{
    ProcessTick();
}

private void ProcessTick()
{
    ...
}

Primarily, I'd do this as direct calling of events seems to me to be a Code Smell - often it indicates spagetti structure code.

Tags:

C#

Timer