请教c#中的Timer控件

blueroute 2010-01-22 10:02:58
我写了一个windows services,从组建中增加了一个Time控件,在timer1_Tick事件中写数据库,代码如下:
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//


protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
this.LogMessage();
}

protected override void OnStop()
{

this.timer1.Enabled = false;
}


private void LogMessage()
{
SqlConnection conn = new SqlConnection("server=127.0.01;database=NetworkServicer;uid=sa;pwd=sa");
string sql_s = " insert into [PingLog] ([EventDatetime],[EventLevel], [EventMessage]) VALUES ('2010-01-21','111','11')";
SqlCommand comm = new SqlCommand(sql_s, conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.LogMessage();
}

每次只有开始时写入的一条记录,timer1_Tick事件怎么没有执行?
...全文
118 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
herichcq 2010-01-22
  • 打赏
  • 举报
回复
mark
jiayp004 2010-01-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 blueroute 的回复:]
我知道原因了,我用的是System.Windows.Forms.Timer ,应该用  System.Threading.Timer
,但是我明明是从工具箱的组建里拖出来的控件呀!
[/Quote]
System.Threading.Timer没做成组件,只是个普通类,不能放到工具栏里。
System.Windows.Forms.Timer继承自Component,可以放在工具栏。
另外在服务程序中应该使用System.Threading.Timer,没界面。
zhenghui2915 2010-01-22
  • 打赏
  • 举报
回复
学习一下,找到解决办法了告诉我一声谢谢。
随风 2010-01-22
  • 打赏
  • 举报
回复
说错了,Timer类型有3种:一种winFome下,一种Web下,一种system.Timer
system.Timer这种默认不显示在工具栏中,只有在工具栏右键菜单choose items,在.net framework控件页下勾选后才显示。
jin20000 2010-01-22
  • 打赏
  • 举报
回复
最后的代码再铺一次
blueroute 2010-01-22
  • 打赏
  • 举报
回复
我的工具箱组件中只有一个TIME,我拖出来的还是System.Windows.Forms.Timer ,奇怪了!
随风 2010-01-22
  • 打赏
  • 举报
回复
工具箱里两种都有
一般还是手工写进去比较保险
blueroute 2010-01-22
  • 打赏
  • 举报
回复
我知道原因了,我用的是System.Windows.Forms.Timer ,应该用 System.Threading.Timer
,但是我明明是从工具箱的组建里拖出来的控件呀!
blueroute 2010-01-22
  • 打赏
  • 举报
回复
windows serives调试很难!
danjiewu 2010-01-22
  • 打赏
  • 举报
回复
你能调试一下吗
blueroute 2010-01-22
  • 打赏
  • 举报
回复
我用的是VS 2008
blueroute 2010-01-22
  • 打赏
  • 举报
回复
我在
protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
this.LogMessage();
}
改为
protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
this.timer1.Start();
this.LogMessage();
}

还是只能写一条记录,事件还是没有触发!
huming_h 2010-01-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 danjiewu 的回复:]
仅仅this.timer1.Enabled = true;是不会启动Timer的,要
this.timer1.Start();
[/Quote]
up
Hamsic 2010-01-22
  • 打赏
  • 举报
回复
貌似写的地方不对 -。-
danjiewu 2010-01-22
  • 打赏
  • 举报
回复
仅仅this.timer1.Enabled = true;是不会启动Timer的,要
this.timer1.Start();
Hamsic 2010-01-22
  • 打赏
  • 举报
回复
time1.Enable = true;

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧