Jump to content
  • Advertisement
Sign in to follow this  
Chris27

[.net] .NET forms.timer

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to have two windows forms timers and start one from the other? I seem to be having a problem with getting the second timer to stop after it is started by the first based on a condition. Timer1 Timer1 tick event if(datetime.now.toshorttime() == "10:00 PM") Timer2.start() Timer2 tick event messagebox.show("Success") Timer2.stop() Timer2 will start, but it will not stop. This is not the case if I Enable Timer2 from a button control. In that case it will output the message once and then stop like it is supposed to. I have a feeling it has something to do with threads, but I know nothing about them. Thanks

Share this post


Link to post
Share on other sites
Advertisement
That is basically the code.

namespace TimerTesting
{
public partial class frmMain : Form
{
private const string strExecuteTime = "11:27 AM";

public frmMain()
{
InitializeComponent();
}

private void btnTest_Click(object sender, EventArgs e)
{
tmrTest.Start();
}

private void tmrTest_Tick(object sender, EventArgs e)
{
MessageBox.Show("Testing");
tmrTest.Stop();
}

private void tmrCheckTime_Tick(object sender, EventArgs e)
{
if (DateTime.Now.ToShortTimeString() == strExecuteTime)
{
tmrTest.Start();
}
}
}
}


Share this post


Link to post
Share on other sites
If you want to know the intervals I set tmrTest to execute every five seconds 5000, and tmrExecuteTime to execute every second 1000.

Share this post


Link to post
Share on other sites
MessageBox.Show is a blocking operation it prevents other code from running. Stop your timer and then show your message box. So just switch the two statements around.

theTroll

Share this post


Link to post
Share on other sites
I don't believe that is the issue as the timer stops if I start it via a button. It has something to do with starting the timer from anther timer. Try it out for yourself in a C# form. Create two visual timer controls and set one to execute something under a certain condition. If the condition is true start the timer. Then call timer.stop in the second timer event. It wont stop even if there is no messagebox. You can test this using a breakpoint. On the otherhand the timer.stop will work if the timer is started via a button control.

Share this post


Link to post
Share on other sites
Does this have anything to do with it?

Quote:
Calling Stop on any Timer within a Windows Forms application can cause messages from other Timer components in the application to be processed immediately, because all Timer components operate on the main application thread. If you have two Timer components, one set to 700 milliseconds and one set to 500 milliseconds, and you call Stop on the first Timer, your application may receive an event callback for the second component first. If this proves problematic, consider using the Timer class in the System.Threading namespace instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!