[.net] .NET forms.timer

Started by
6 comments, last by Chris27 15 years, 10 months ago
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
Advertisement
Want to show us some code?
Mike Popoloski | Journal | SlimDX
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();            }        }     }}
If you want to know the intervals I set tmrTest to execute every five seconds 5000, and tmrExecuteTime to execute every second 1000.
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
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.
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.
Mike Popoloski | Journal | SlimDX
Ok I will try the system.threading.timer and see if it works any better.

Thank you

This topic is closed to new replies.

Advertisement