我在Windows服务的项目里用到了一个Timer控件,发现在Windows服务(我这是VS2005)里,Timer控件的Tick事件没有作用。服务已经启动了但实际并未对数据库产生操作。请问这是为什么?Timer的Enalbed我已经设成了True. Interval我设成了1000。Timer中取自组件栏里的。代码是这样的:
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.timer1.Enabled = true;
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this.timer1.Enabled = false;
} private void timer1_Tick(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=(local);database=sxl;uid=sa;pwd=;");
SqlCommand comm = new SqlCommand("insert into sxlxxy(WEIJIBIANMA,NASHUIRENJC) values('111','111')", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.timer1.Enabled = true;
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this.timer1.Enabled = false;
} private void timer1_Tick(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=(local);database=sxl;uid=sa;pwd=;");
SqlCommand comm = new SqlCommand("insert into sxlxxy(WEIJIBIANMA,NASHUIRENJC) values('111','111')", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
解决方案 »
- 二进制加法如何实现
- c#中关于contextmenu隐藏问题
- 两个gridview间的事件
- C过请进,多个DataTable中的数据导入到一个Excel中出错.(C#)
- OracleDataAdapter.Update 批量新增时只新增了一部分数据
- 菜鸟问:request/response在哪个包里?(马上揭贴!)
- 问个关于Font的问题
- c#中不能用<%# Container.DataItem("字段名") %>吗?
- UDP音频传送问题
- 关于Update的问题,TheAres(班门斧)请进!
- dataTable问题
- gg.ascx 获取type的值<uc1:gg id="Gg1" runat="server" type="00"></uc1:gg>??
用System.Timers.Timer或者线程的计时器System.Threading.Timer
建议用System.Threading.Timer