Reputation: 89
Is the first time that I use timer, so probably I'm doing something wrong.
My code is something like this:
private void timer1_Tick(object sender, EventArgs e)
{
button2.PerformClick();
}
private void button2_Click(object sender, EventArgs e)
{
// random code here
timer1.Interval = 5000;
timer1.Start();
timer1_Tick(null,null);
}
What I want to do with this is: execute the random code, then wait the timer interval and execute the Tick (that will do a 'click' in the button again, for execute again the same), and repeat this for ever.
Sorry if is a easy mistake, I'm starting with this and don't know what I'm doing wrong.
Thanks you for read me! :D
Upvotes: 0
Views: 2066
Reputation: 216243
A Timer event (for every kind of timers) doesn't need to be manually called by you.
You set its event handler method, set the interval and start it.
The underlying framework calls your Tick
event when it is time to call it.
So you need to put your random code in a sub that you could call from the Tick
event and from your button click event. Also, you should think to block further activations of the timer. You could disable the button and, when you have finished with your random code and the conditions are true, stop the timer and reenable the button.
private void button2_Click(object sender, EventArgs e)
{
stopTheTimer = false;
YourCommonMethod();
button2.Enabled = false;
timer1.Tick += timer1_Tick
timer1.Interval = 5000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
YourCommonMethod();
}
private void YourCommonMethod()
{
// execute your 'random' code here
if(stopTheTimer)
{
timer1.Stop();
timer1.Tick -= timer1_Tick; // disconnect the event handler
button2.Enabled = true;
}
}
Upvotes: 2
Reputation: 39122
Wire up the Tick() event only once, preferably thru the IDE so you don't end up with multiple handlers and your code running more than once for each Tick() event.
*In the example below I've wired it up in the constructor as an alternative...but don't do this and wire it up thru the IDE or it will fire twice for each Tick().
You probably just want to call Start() in the Button handler and then call your "random code" in the Tick() event like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = false;
timer1.Interval = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
timer1.Tick += timer1_Tick; // just wire it up once!
}
private void button1_Click(object sender, EventArgs e)
{
if (!timer1.Enabled)
{
Foo(); // <-- Optional if you want Foo() to run immediately without waiting for the first Tick() event
timer1.Start();
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
Foo();
}
private void Foo()
{
Console.WriteLine("Random Code @ " + DateTime.Now.ToString());
}
}
This isn't that much different than the other answers, but wiring up the Tick() event multiple times is a serious flaw...
Upvotes: 1
Reputation: 1973
Here's you go. . . .
private void button2_Click(object sender, EventArgs e)
{
// random code here
timer1.Interval = 5000;
timer1.Start();
timer1.Tick += timer1_Tick;
}
void timer1_Tick(object sender, EventArgs e)
{
//Your timing code here.
}
Upvotes: 1