0

I'm developing an application to send SMS via AT commands, that part is OK. I have a list of contacts and I want to send a file (which changes in time) to all of my contacts. In order to do that I need to repeat the sending part every 30 minutes. I found this code using a timer, but I'm not sure if it's useful in my case and how I can use it. Please help, any idea is appreciated.

private void btntime_Click(object sender, EventArgs e)
    {
        s_myTimer.Tick += new EventHandler(s_myTimer_Tick);
        int tps = Convert.ToInt32(textBoxsettime.Text);

        // 1 seconde = 1000 millisecondes
        try
        {
            s_myTimer.Interval = tps * 60000;
        }
        catch
        {
            MessageBox.Show("Error");
        }
        s_myTimer.Start();

        MessageBox.Show("Timer activated.");

    }

    // Méthode appelée pour l'évènement
    static void s_myTimer_Tick(object sender, EventArgs e)
    {
        s_myCounter++;

        MessageBox.Show("ns_myCounter is " + s_myCounter + ".");

        if (s_myCounter >= 1)
        {
            // If the timer is on...
            if (s_myTimer.Enabled)
            {
                s_myTimer.Stop();
                MessageBox.Show("Timer stopped.");
            }
            else
            {
                MessageBox.Show("Timer already stopped.");
            }
        }
    }
4

3 回答 3

1

Whether this code is useful or not depends entirely what you want to do with it. It shows a very basic usage of the Timer-class in .NET, which is indeed one of the timers you can use if you want to implement a repeating action. I suggest you look at the MSDN-guidance on all timers in .NET and pick the one that best fits your requirements.

于 2013-07-12T08:28:37.420 回答
0

你可以开始这样的事情。发送完所有短信后,30 秒后将再次发送短信。

    public Form1()
    {
        InitializeComponent();
        timer1.Enabled = true;
        timer1.Interval = (30 * 60 * 1000);
        timer1.Tick += SendSMS;
    }

    private void SendSMS(object sender, EventArgs e)
    {
        timer1.Stop();
        // Code to send SMS
        timer1.Start();
    }

希望能帮助到你。

于 2013-07-12T08:53:14.300 回答
0

这很简单,但如果您不需要非常准确的发射时间跨度,它应该适合您。在表单中添加一个计时器 (timer1) 和一个计时器滴答事件。

private void btntime_Click(object sender, EventArgs e)
    {
        timer1.Tick += new EventHandler(timer1_Tick);

            timer1.Interval = 30 *1000;
            timer1.Start();



    }

    private void timer1_Tick(object sender, EventArgs e)
    {

         timer1.Stop();
        //fire you method to send the sms here 
        MessageBox.Show("fired");//take this away after test

        timer1.Start();



    }
于 2013-07-12T08:48:49.193 回答