我想开发一个 Windows 服务,它将检测按下的键,或者它会在按下键时启动。那可能吗?
我创建了一个服务,该服务在一段时间后运行并更新一些数据库表。
这是我不时更新数据库所做的代码。
System.Timers.Timer timer1 = new System.Timers.Timer();
private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(timer1)).BeginInit();
timer1.Enabled = true;
((System.ComponentModel.ISupportInitialize)(timer1)).EndInit();
}
protected override void OnStart(string[] args)
{
try
{
WriteLog("test Services Started at : " + System.DateTime.Now);
// Time Elapsed event
timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
int intOnElapsedTime = Convert.ToInt32(
System.Configuration.ConfigurationManager
.AppSettings["intOnElapsedTime"]
.ToString());
timer1.Interval = 1000 * 10 * intOnElapsedTime;
timer1.Enabled = true;
}
catch (Exception ex)
{
WriteErrorLog(ex.Message, ex.StackTrace, "OnStart");
}
}
private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
try{ /* update database table */}
catch (Exception exp){ ...}
}