C# 创建windows服务(Windows Service)并定时执行
2013-08-27 15:54
405 查看
来源:bbwfang 网上整理并添加自己使用的心得
一、创建window服务
1、新建项目-->选择Windows服务。默认生成文件包括Program.cs,Service1.cs
2、在Service1.cs添加如下代码:
System.Timers.Timer timer1; //计时器
public Service1()
{
InitializeComponent();
System.Timers.Timer t = new System.Timers.Timer();
t.Interval =1000*15;
t.Elapsed += new System.Timers.ElapsedEventHandler(RunWork);
t.AutoReset = true;//设置是执行一次(false),还是一直执行(true);
t.Enabled = true;//是否执行
}
protected override void OnStart(string[] args) //服务启动执行
{
this.timer1.Start();
}
protected override void OnStop() //服务停止执行
{
this.timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
//执行SQL语句或其他操作
}
二、添加window服务安装程序
1、打开Service1.cs【设计】页面,点击右键,选择【添加安装程序】,会出现serviceInstaller1和serviceProcessInstaller1两个组件
2、将serviceProcessInstaller1的Account属性设为【LocalSystem】, serviceInstaller1的StartType属性设为【Automatic】,ServiceName属性可设置服务名称,此后在【管理工具】--》【服务】中即显示此名称
3、ProjectInstaller.cs文件,在安装服务后一般还需手动启动(即使上述StartType属性设为【Automatic】),可在ProjectInstaller.cs添加如下代码实现安装后自动启动
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
//参数为服务的名字
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("服务名称");
controller.Start();
}
三、安装、卸载window服务
1、输入cmd(命令行),输入cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319,2.0为cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
2、安装服务(项目生成的exe文件路径)
InstallUtil "E:\WindowsService1\bin\Debug\WindowsService1.exe"
3、卸载服务
InstallUtil /u "E:\WindowsService1\bin\Debug\WindowsService1.exe"
四、查看window服务
控制面板-->管理工具-->服务,可在此手动启动,停止服务
五、调试window服务
1、通过【事件查看器】查看
2、直接在程序中调试(菜单-->调试-->附加进程-->服务名(这里的服务名是项目名称,不是ServiceName属性自定义的名称,所以建议自定义名称和项目名称保持一致,另外需勾选【显示所有用户的进程】才能看到服务名)-->附加
3. 在程序中打断点调试即可,另外调试服务时服务必须已启动(管理工具-->服务)
一、创建window服务
1、新建项目-->选择Windows服务。默认生成文件包括Program.cs,Service1.cs
2、在Service1.cs添加如下代码:
System.Timers.Timer timer1; //计时器
public Service1()
{
InitializeComponent();
System.Timers.Timer t = new System.Timers.Timer();
t.Interval =1000*15;
t.Elapsed += new System.Timers.ElapsedEventHandler(RunWork);
t.AutoReset = true;//设置是执行一次(false),还是一直执行(true);
t.Enabled = true;//是否执行
}
protected override void OnStart(string[] args) //服务启动执行
{
this.timer1.Start();
}
protected override void OnStop() //服务停止执行
{
this.timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
//执行SQL语句或其他操作
}
二、添加window服务安装程序
1、打开Service1.cs【设计】页面,点击右键,选择【添加安装程序】,会出现serviceInstaller1和serviceProcessInstaller1两个组件
2、将serviceProcessInstaller1的Account属性设为【LocalSystem】, serviceInstaller1的StartType属性设为【Automatic】,ServiceName属性可设置服务名称,此后在【管理工具】--》【服务】中即显示此名称
3、ProjectInstaller.cs文件,在安装服务后一般还需手动启动(即使上述StartType属性设为【Automatic】),可在ProjectInstaller.cs添加如下代码实现安装后自动启动
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
//参数为服务的名字
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("服务名称");
controller.Start();
}
三、安装、卸载window服务
1、输入cmd(命令行),输入cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319,2.0为cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
2、安装服务(项目生成的exe文件路径)
InstallUtil "E:\WindowsService1\bin\Debug\WindowsService1.exe"
3、卸载服务
InstallUtil /u "E:\WindowsService1\bin\Debug\WindowsService1.exe"
四、查看window服务
控制面板-->管理工具-->服务,可在此手动启动,停止服务
五、调试window服务
1、通过【事件查看器】查看
2、直接在程序中调试(菜单-->调试-->附加进程-->服务名(这里的服务名是项目名称,不是ServiceName属性自定义的名称,所以建议自定义名称和项目名称保持一致,另外需勾选【显示所有用户的进程】才能看到服务名)-->附加
3. 在程序中打断点调试即可,另外调试服务时服务必须已启动(管理工具-->服务)
相关文章推荐
- C#创建windows服务并定时执行
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C#创建windows服务并定时执行
- C#创建windows服务并定时执行
- C#创建简单的windows服务项目--定时执行任务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C# 创建Windows Service服务 Windows服务
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- C#创建windows服务并定时执行
- C#创建windows服务并定时执行
- C#创建windows服务并定时执行
- 使用C#创建WindowsService(Windows服务):定期执行Dos命令或Bat(批处理)文件
- C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务
- C#创建windows服务并定时执行
- 用C#创建Windows服务(Windows Services)
- C# 创建Windows服务。服务功能:定时操作数据库
- [转]用VS(c#)创建、调试windows service以及部署卸载