怎么实现定时触发事件 比如想定时向数据库中插入数据
2018-01-10
展开全部
添加一个全敏野局应用程序类,在里面岁庆添加如下代码:
void Application_Start(object sender, EventArgs e) {
Timer t = new Timer(3000); //每隔3秒
t.Enabled = true;
t.AutoReset = true;
t.Elapsed += new ElapsedEventHandler(aaa);
GC.KeepAlive(t);
}
//把下桥雀喊面方法中改成你的数据库操作即可
public void aaa(object sender, ElapsedEventArgs e)
{ FileStream fs = new FileStream("d://a.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(DateTime.Now);
sw.Close();
fs.Close();
}
void Application_Start(object sender, EventArgs e) {
Timer t = new Timer(3000); //每隔3秒
t.Enabled = true;
t.AutoReset = true;
t.Elapsed += new ElapsedEventHandler(aaa);
GC.KeepAlive(t);
}
//把下桥雀喊面方法中改成你的数据库操作即可
public void aaa(object sender, ElapsedEventArgs e)
{ FileStream fs = new FileStream("d://a.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(DateTime.Now);
sw.Close();
fs.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询