windows服务里可以使用timer吗?

vivianfdlpw 2004-04-13 01:54:07
是不是不可以阿?我这个貌似不行,timer1不起作用

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
thread=new System.Threading.ThreadStart (run);
t=new System.Threading.Thread (thread);
t.Start ();
}
protected void run()
{
timer1.Enabled =true;
timer1.Interval =3;
timer1.Tick +=new EventHandler(timer1_Tick);



}

/// <summary>
/// 停止此服务。
/// </summary>
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
t.Abort ();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
try
{

System.IO .FileStream fs=null;
if(System.IO .File .Exists (@"c:\starspring.txt")==false)
{
fs=System.IO .File .Create (@"c:\starspring.txt");
}
else
{
fs=System.IO .File.OpenWrite (@"c:\starspring.txt");
}

string str="hello,this is a test";
byte[] buffer=System.Text .Encoding .Default .GetBytes (str);
fs.Write (buffer,0,buffer.Length );

fs.Flush ();
fs.Close ();
}
catch(Exception ex)
{
System.Windows .Forms .MessageBox .Show (ex.Message );
}


}
}
...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2004-04-13
  • 打赏
  • 举报
回复
To CMIC(大象):

谢谢,原来是这个原因啊,土了:))
vivianfdlpw 2004-04-13
  • 打赏
  • 举报
回复
能帮我看一下这段代码吗?timer1不起作用阿,谢谢:)
CMIC 2004-04-13
  • 打赏
  • 举报
回复
用工具箱->组建->Timer, 不能用工具箱->windows窗体->Timer
designonline 2004-04-13
  • 打赏
  • 举报
回复
绝对可以,我测试过

110,534

社区成员

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

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

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