Far*_*rna 3 c# windows-services
我正在写一个Windows服务应用程序,我使用计时器控件.在我的Windows服务的OnStart()事件中,我启动计时器,我希望每隔一分钟调用一次StartTimer(),但什么也没发生.
这有什么不对?
谢谢.
myWinService.cs :::
protected override void OnStart(string[] args)
{
timer1.Interval=60000;
timer1.Start();
}
private void StartTimer()
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(Environment.UserName.ToString()+tik.ToString());
m_streamWriter.Flush();
}
private void timer1_Tick(object sender, EventArgs e)
{
tik++;
StartTimer();
}
Run Code Online (Sandbox Code Playgroud)
正如@Gunner的评论所指出的那样,你还没有联系到这个Timer.Tick事件.
在您的OnStart方法中,您需要timer1_Tick使用Tick事件注册方法:
timer1.Tick += new EventHandler(timer1_Tick);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |