c中的重复动作
本文关键字: | 更新日期: 2023-09-27 18:22:08
我正在开发一个通过AT命令发送短信的应用程序,这部分还可以。我有一个联系人列表,我想向我的所有联系人发送一个文件(随时间变化)。为了做到这一点,我需要每30分钟重复一次发送部分。我用定时器找到了这个代码,但我不确定它对我的情况是否有用,以及我如何使用它。请帮忙,任何想法都将不胜感激。
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.");
}
}
}
这段代码是否有用完全取决于你想用它做什么。它展示了Timer类在.NET中的一个非常基本的用法,如果你想实现重复操作,它确实是你可以使用的计时器之一。我建议您查看MSDN关于.NET中所有计时器的指南,并选择最适合您需求的计时器。
这很简单,但如果您不需要一个非常准确的发射时间间隔,则应该适用。在表单中添加一个计时器(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();
}
您可以开始这样的操作。发送完所有短信后,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();
}
希望能有所帮助。