在windows服务里如何使用timer
2013-07-26 19:29
411 查看
//在服务里使用timer的时候,我们应该引进一个命名空间 using System.Timers; 而非控件里的 timer 控件,控件里的 timer 控件使用的命名空间不是这个!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Com.Hcl.ProvinceDataServiceClient.DAL;
using System.Timers;
namespace GuangDongWindowsService
{
public partial class GuangDongService : ServiceBase
{
public GuangDongService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
System.Timers.Timer t = new Timer(1000); //动态的生成timer 参数是设置 timer的 每隔一段时间执行,
t.Start(); // 打开timer,否则是不可用的
t.Elapsed+=new ElapsedEventHandler(T_click); // 给timer加一个事件
t.AutoReset = true; // 设置可以多次执行
t.Enabled = true; //开始执行timer
}
catch(Exception ex)
{
throw(ex);
}
}
void T_click(object sender, ElapsedEventArgs e)
{
using (System.IO.StreamWriter rs = new System.IO.StreamWriter(@"D:\log.txt", true))
{
rs.WriteLine(DateTime.Now.ToString());
}
}
protected override void OnStop()
{
//using (System.IO.StreamWriter rs = new System.IO.StreamWriter("d;\\log.txt", true))
//{
// rs.WriteLine(DateTime.Now.ToString("yy-mm-dd HH:mm:ss") + "Stop.");
//}
//FormStop();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Com.Hcl.ProvinceDataServiceClient.DAL;
using System.Timers;
namespace GuangDongWindowsService
{
public partial class GuangDongService : ServiceBase
{
public GuangDongService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
System.Timers.Timer t = new Timer(1000); //动态的生成timer 参数是设置 timer的 每隔一段时间执行,
t.Start(); // 打开timer,否则是不可用的
t.Elapsed+=new ElapsedEventHandler(T_click); // 给timer加一个事件
t.AutoReset = true; // 设置可以多次执行
t.Enabled = true; //开始执行timer
}
catch(Exception ex)
{
throw(ex);
}
}
void T_click(object sender, ElapsedEventArgs e)
{
using (System.IO.StreamWriter rs = new System.IO.StreamWriter(@"D:\log.txt", true))
{
rs.WriteLine(DateTime.Now.ToString());
}
}
protected override void OnStop()
{
//using (System.IO.StreamWriter rs = new System.IO.StreamWriter("d;\\log.txt", true))
//{
// rs.WriteLine(DateTime.Now.ToString("yy-mm-dd HH:mm:ss") + "Stop.");
//}
//FormStop();
}
}
}
相关文章推荐
- 在windows服务里如何使用timer
- Windows 服务中使用 Timer 控件时,Tick 事件不命中问题的解决
- 如何在 Windows 服务中正确使用 log4net
- 在Windows服务中如何使用计时器?
- Windows 服务中使用 Timer 控件时,Tick 事件不命中问题的解决
- 让服务不再停止(在Windows Service中使用Timer的技巧)
- C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)(从iteye搬到博客园)
- windows服务中的定时器timer的使用
- [摘抄]windows服务中的定时器timer使用
- 让服务不再停止(在Windows Service中使用Timer的技巧)
- 在 Windows 服务中使用 Timer 的问题
- C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)
- 让服务不再停止(在Windows Service中使用Timer的技巧)
- MS Windows Timer的限制以及如何使用更高分辨率的计时手段。
- 如何在企业环境中使用 Windows 部署服务 部署 Windows XP(实验手册)
- Delphi的TService的輸入桌面切換(服务程序)(windows登录界面如何截图)(使用了OpenDesktop和GetThreadDesktop等API)
- 让服务不再停止(在Windows Service中使用Timer的技巧)
- 让服务不再停止(在Windows Service中使用Timer的技巧)
- C#创建windows服务搭配定时器Timer使用实例
- 如何在 Windows 服务中正确使用 log4net