Hi Everyone,

I'm making a windows mobile app in C# that aims to help those affected by Alzheimer's Disease

Part of it needs to send text messages at 5 minute intervals, however, the rest of the C# program needs to continue at the same time.

I know that when you set timers in Visual Basic, for example, you can set lots of them and they work indipendantly to the program, so that when that timer reaches its interval (when it "ticks"), the events associated with it would be triggered, rather than pausing *everything* until the tick occurs.

Is this the same in C#?

What is the best way to get an event to happen at a time interval while letting the rest of the program continue?

Recommended Answers

All 3 Replies

Yes timers do work indepently. Each timer you create has a tick event and whatever code you place there will run when the interval expires.

I found when using timer in recent project that the timer control from the toolbox in VS did not work for some reason. I had to code it based on some samples I found.

System.Timers.Timer timer = null;
        Double timeConversion;
        int interval = 24;

private void buttonStart_Click(object sender, EventArgs e)
        {
            IpUpdater();
            timeConversion = interval * 60 * 60 * 1000; //convert hours to milliseconds
            timer = new System.Timers.Timer();
            timer.Enabled = true;
            timer.Interval = timeConversion;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            IpUpdater();
        }

With this I call my IpUpdater method when I click the start button within my app and then IpUdater is called once every 24 hours.

You can call

timer.enabled = false

to stop the timer.

commented: Good suggestion. +8

I'm not sure what was wrong with your timer corey, i dropped on onto a form with two buttons and a textbox and the following code worked fine:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            txtOutput.Text = "Timer Started at " + DateTime.Now.ToString();
            timer1.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            txtOutput.Text += Environment.NewLine + "Timer Stopped at " + DateTime.Now.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            txtOutput.Text += Environment.NewLine + "Timer Ticked at " + DateTime.Now.ToString();
        }
    }

I posted late last night and forgot to mention that I was using the timer in a windows service project. You are correct the one for windows forms works but the one under Component in the toolbox did not seem to function. I found serveral forum posts where people had problems with that one and had to code the timer manually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.