使用VS2010(C#语言)创建Windows Service详细图解
来源:互联网 发布:星星知我心电视剧全集1 编辑:程序博客网 时间:2024/06/06 02:30
步骤如下:
1、 打开VS2010,文件/新建/项目,在新建项目框中 选择Windows Service 或 Windows服务;
2、 双击打开Service1.cs,此时看到的是设计视图,点击菜单栏的 工具/选择工具箱项,如图1所示,在.NET Framework组件中找到Timer,系统默认的是System.Windows.Forms命名空间的Timer,而我们需要的是System.Timers命名空间的Timer,所以选中这个Timer,点击确定,如图中红线框所示:
图1 选择工具箱项
3、 回到Service1[设计],在左侧 工具箱中展开 组件,双击第二个Timer(如图2),在页面选中刚添加的Timer,右键选择属性,在属性窗口的第一行 看看是不是System.Timers.Timer(如图3);
图2 左侧工具箱
图3 右侧属性窗口
4、 在Timer的属性窗口中,可以修改Timer的名称和间隔时间Interval(单位毫秒);点击事件标识(闪电图标),双击 Elapsed 右侧的文本框,生成Timer 事件;
5、 在Service1[设计] 的空白处,右键 查看代码,在类名下面声明 线程变量thdMain(变量名可用其他名称),在OnStart方法中给线程thdMain定义,启动Timer,并启动线程,Service1.cs全部代码如下:
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 System.Threading;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
//privatebool blnStopThread;
privateThread thdMain;
publicService1()
{
InitializeComponent();
}
protectedoverridevoidOnStart(string[] args)
{
thdMain = newThread(newThreadStart(timer1.Start));
thdMain.Start();
}
protectedoverridevoidOnStop()
{
}
privatevoid timer1_Elapsed(objectsender, System.Timers.ElapsedEventArgs e)
{
Class1test = new Class1();
test.Test(); //业务操作
}
}
}
6、 在timer1_Elapsed中编写你要做的业务操作,本例中的业务操作在 Class1.Test()方法中;
7、 Class1的代码如下,向数据库写入记录:
class Class1
{
public void Test()
{
stringconstr = @"Data Source=.\MSSQL;Initial Catalog=Test;IntegratedSecurity = SSPI;";
Randomrdm = new Random();
stringnum = rdm.Next(1, 1000).ToString();
stringname = "Service" + num;
stringcmdstr = "insert into dbo.Table_1 (name,value) values('"+name +"','"+num +"')";
SqlCommandcmd = new SqlCommand();
using(SqlConnection conn =new SqlConnection(constr))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
}
}
8、 在Service1.cs[设计] 页面的空白处右键 属性,修改ServiceName,如图4:
图4 在Service1的属性窗口中修改服务名称
9、 在Service1[设计] 中右键 添加安装程序,系统自动添加了ProjectInstaller.cs 文件,在ProjectInstaller.cs[设计]中有2个组件:serviceInstaller1 和 serviceProcessInstaller1;
10、 选中serviceInstaller1 右键属性,在属性窗口中设置ServiceName 和StartType,这里的ServiceName 必须和Service1中的ServiceName相同,如图5所示:
图5 设置serviceInstaller1的属性
11、 选中serviceProcessInstaller1 右键属性,Account 选中LocalSystem,LocalSystem 是服务控制管理员使用的账号,只要有Windows的管理员权限就可以使用这个账户,如图6:
图6 设置启动服务所使用的账号
12、 保存项目,到此已经完成Windows Service;点击菜单栏生成/生成WindowsService;
13、 打开项目文件夹,在bin/Debug 文件夹中找到已编译文件WindowsService.exe应用程序,保证应用程序所在目录中不能有空格,否则把应用程序复制到其他目录中;
14、 下面安装服务
返回桌面,打开 菜单/所有程序/MiscrosoftVisual Studio 2010/ Visual Studio Tools/ Visual Studio 命令提示(2010),需要已管理员身份运行,在命令窗口中输入 installutil 目录\WindowsService.exe ,如图7,安装完成后,可以在控制面板/管理工具/服务中找到刚安装的服务MyTest2,右键启动,可以在数据库中查看效果。
图7 在Visual Studio 命令提示(2010) 窗口中安装
15、 卸载服务,只需在installutil 后加个参数 /u 即卸载, installutil /u 目录\WindowsService.exe 。
注:该示例是使用计时器控制业务程序的按指定时间间隔执行,还可以使用循环和等待(sleep)来控制程序间隔执行。
- 使用VS2010(C#语言)创建Windows Service详细图解
- C#创建Windows Service
- c#创建Windows Service
- C#创建Windows Service
- C#创建windows服务图解
- Windows ATL Service 服务的创建(VS2010)
- VS2010 C# 创建Windows服务
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- c#创建windows service示例
- 用C#创建Windows Service
- c#创建windows service示例 .
- C# VS2010服务(service)
- ios layoutsubviews
- js对象属性方法大总结
- XML 解析之QXmlStreamReader
- 浅谈领导和领导力
- 第一次做c++项目的一些总结
- 使用VS2010(C#语言)创建Windows Service详细图解
- ORACLE 处理长时间锁表
- Java压缩解压ZIP之Zip4j入门 (键人岐)
- 10款最出色的免费数据库管理工具
- Sublime Text 2 设置文件详解
- Java 理论与实践: 描绘线程安全性
- 模糊查询与索引的性能【有误指正】
- 几个有意思的算法
- 常用正则表达式大全!(例如:匹配中文、匹配html)