windows服务里可以使用timer吗?
是不是不可以阿?我这个貌似不行,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 );
}
}
}