I have a Windows.Forms.Timer
in my code, that I am executing 3 times. However, the timer isn't calling the tick function at all.
private int count = 3;
private timer;
void Loopy(int times)
{
count = times;
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
count--;
if (count == 0) timer.Stop();
else
{
// Do something here
}
}
Loopy()
is being called from other places in the code.
Here's an Rx ticker that works:
Of course, you can subscribe something more useful in your program.
If the method Loopy() is called in a thread that is not the main UI thread, then the timer won't tick. If you want to call this method from anywhere in the code then you need to check the
InvokeRequired
property. So your code should look like (assuming that the code is in a form):If you are using Windows.Forms.Timer then should use something like following.
I am not sure what you are doing wrong it looks correct, This code works: See how it compares to yours.
Try using System.Timers instead of Windows.Forms.Timer