【发布时间】:2014-08-29 10:38:40
【问题描述】:
我有一种情况,我应该通过 Windows 服务和 Timer 来解决。这里是描述。我有一个 winform,用户可以在其中添加他想要报告的时间。他将添加什么时间保存到数据库中。现在这意味着多个用户,所以在数据库中会有多个相同的添加。现在根据我的要求,我必须在他设置的特定时间向用户发送报告。我必须设置我的计时器,这样每当数据库中的适当时间到来时,报告都应该发送给用户。在此之后,它应该再去一次并发送它等等。 为此,我正在使用 Windows 服务并尝试通过执行数据库连接和查询来获得我们需要设置计时器的第一次时间。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using MySql.Data.MySqlClient;
namespace AutomailTrigger
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
string time;
string conn = "Server=localhost;Port=3306;Database=;UID=root;Pwd=;pooling=false";
string Query = "SELECT * FROM `reportsetting` order by SendingTime desc limit 1;";
MySqlConnection con = new MySqlConnection(conn);
MySqlCommand comm = new MySqlCommand(Query, con);
con.Open();
MySqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
time = dr["time"].ToString();
}
this.timer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer.
this.timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
this.timer.Enabled = true;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Your code when the timer elapses
}
protected override void OnStop()
{
}
}
}
现在我有一个疑问,如何在其中添加计时器以保持循环移动.. 请帮忙,因为这对我来说是合乎逻辑的障碍..
【问题讨论】:
标签: c# timer windows-services