需求:每半小时发送一次电子邮件,这个该如何判断呢?我写了一个例子,没两分钟发送一次:
static void Main(string[] args)
{ DateTime dt1 = new DateTime(2012, 7, 25, 9, 11, 0); DateTime dt2 = new DateTime(2012, 7, 25, 9, 13, 0);
while (1 == 1)
{
dt2 = DateTime.Now;
if ((dt2 - dt1).Ticks % 1200000000 == 0)
{
Console.WriteLine(dt2.ToString());
}
}
Console.Read();
}
但是一次会发送多条,条数是不规律的,该如何解决呢?
static void Main(string[] args)
{ DateTime dt1 = new DateTime(2012, 7, 25, 9, 11, 0); DateTime dt2 = new DateTime(2012, 7, 25, 9, 13, 0);
while (1 == 1)
{
dt2 = DateTime.Now;
if ((dt2 - dt1).Ticks % 1200000000 == 0)
{
Console.WriteLine(dt2.ToString());
}
}
Console.Read();
}
但是一次会发送多条,条数是不规律的,该如何解决呢?
{
public Form2()
{
InitializeComponent(); TimerCallback timerCallback = new TimerCallback(sendMail); System.Threading.Timer timer = new System.Threading.Timer(timerCallback, null, 0, 1 * 1000 * 60 * 30); } public static void sendMail(object obj)
{
using (StreamWriter w = File.AppendText(Application.StartupPath + "\\log.txt"))
{
w.Write("\r\n发送邮件 : " + DateTime.Now.ToLongTimeString()); w.WriteLine("-------------------------------"); w.Flush();
}
// 加载数据库未发送邮件,发送后及时改写邮件状态为已发送 }
{
System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Interval = 1000; //方法运行间隔时间(毫秒为单位);
t.AutoReset = true;
t.Enabled = true;
} void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
发送邮件();
}这样就会每隔1秒自动发送了。
建站交流群:61918700