Reputation: 398
My application looks like this:
I have an MDI parent form - form1, the form that starts and stops the timer - form 2, and a timerCalls class with all the timer logic.
Form1 code:
TimerCalls timerCalls = new TimerCalls();
public SMS()
{
InitializeComponent();
timerCalls.InitializeTimer();
}
Form2 code:
TimerCalls timerCalls = new TimerCalls();
public Form2()
{
InitializeComponent();
}
private void btnSendOn_Click(object sender, EventArgs e)
{
timerCalls.sendTimer.Start();
}
private void btnSendOff_Click(object sender, EventArgs e)
{
timerCalls.sendTimer.Stop();
}
TimerCalls class code:
class TimerCalls
{
public System.Timers.Timer sendTimer = new System.Timers.Timer();
public System.Timers.Timer recTimer = new System.Timers.Timer();
public void InitializeTimer()
{
// Send timer
sendTimer.Elapsed += new ElapsedEventHandler(sendProcessTimerEvent);
sendTimer.Interval = 3000;
//rec timer
recTimer.Elapsed += new ElapsedEventHandler(recProcessTimerEvent);
recTimer.Interval = 3000;
}
private void sendProcessTimerEvent(object sender, EventArgs e)
{
MessageBox.Show("Send 3 sec");
}
private void recProcessTimerEvent(object sender, EventArgs e)
{
MessageBox.Show("Rec 3 sec");
}
}
This is the problem: I open form2, start the timer, close form2, open it again and try to stop the timer it doesn't stop it. Once I reopen the form2, all I can do is start and stop a new timer, but the previous one is still running. Everything works fine (timer starting and stopping) until I close form2. If the timer was on when I closed the form I can't stop it once I open the form2 again.
How can I fix this?
Upvotes: 0
Views: 728
Reputation: 27962
You are creating multiple instances of TimerCalls
— in other words, there is no single time instance, there are many. You can't expect stopping a running timer when you call stop for a different instance.
Create a singleton instance of TimerCalls
and that will solve your problem.
Upvotes: 0
Reputation: 1443
After you closed Form2 and reopened it, this is run again:
public System.Timers.Timer sendTimer = new System.Timers.Timer();
public System.Timers.Timer recTimer = new System.Timers.Timer();
public void InitializeTimer()
{
// Send timer
sendTimer.Elapsed += new ElapsedEventHandler(sendProcessTimerEvent);
sendTimer.Interval = 3000;
// Rec timer
recTimer.Elapsed += new ElapsedEventHandler(recProcessTimerEvent);
recTimer.Interval = 3000;
}
Your first timer runs on a different thread and it's not stopped and when you open Form2 for the second time a new timer is initialized so now you have two timers and you can control only the last one.
Upvotes: 0
Reputation: 3139
Assuming you are closing the form, and then creating a new one then yes, it will create multiple instances of Form2.
The best approach as suggested by others would be to use a singleton pattern for TimerCalls and just get the instance in your Form2.
If this sounds like too much work, simply hide Form2 instead of closing it:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing)
return;
e.Cancel = true;
Hide();
}
Then just make sure when you click the button to reopen Form2, you just show the previously created one - which will be something like this.
public class Form1 : Form
{
private Form2 mForm2;
protected void OpenForm2_Click(object sender, EventArgs e)
{
if (mForm2 == null)
mForm2 = new Form2();
mForm2.Show();
}
}
Upvotes: 2
Reputation: 14387
Each time you open a new Form2 a new timer gets initialized. That will be the one you are starting and stopping. I would advise to either make your Timer class a singleton, or pass a reference of the a single timerCalls instance from Form1 to each new Form 2
Upvotes: 0