创建Windows服务的几种方式总结

目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类Serv
【菜科解读】
最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来。
目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshelfc.利用小工具instsrv和srvany
下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志:
a.利用.net框架类ServiceBase
本方式特点:简单,兼容性好
通过继承.net框架类ServiceBase实现
第1步: 新建一个Windows服务
public partial class Service1 : ServiceBase readonly Timer _timer; private static readonly string FileName = Path.GetDirectoryName ( Assembly.GetExecutingAssembly ( ).Location ) + @"\" + "test.txt"; public Service1 ( ) InitializeComponent ( ); _timer = new Timer ( 5000 ) AutoReset = true , Enabled = true _timer.Elapsed += delegate ( object sender , ElapsedEventArgs e ) this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) ); protected override void OnStart ( string [ ] args ) this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) ); protected override void OnStop ( ) this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine ); void witre ( string context ) StreamWriter sw = File.AppendText ( FileName ); sw.WriteLine ( context ); sw.Flush ( ); sw.Close ( ); }
第2步: 添加Installer
[RunInstaller ( true )] public partial class Installer1 : System.Configuration.Install.Installer { private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; public Installer1 ( ) { InitializeComponent ( ); processInstaller = new ServiceProcessInstaller ( ); serviceInstaller = new ServiceInstaller ( ); processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = "my_WindowsService"; serviceInstaller.Description = "WindowsService_Description"; serviceInstaller.DisplayName = "WindowsService_DisplayName"; Installers.Add ( serviceInstaller ); Installers.Add ( processInstaller ); } }
第3步:安装,卸载 Cmd命令installutil WindowsService_test.exe (安装Windows服务)installutil /u WindowsService_test.exe (卸载Windows服务)
代码下载:http://files.cnblogs.com/aierong/WindowsService_test.rar
b.利用组件Topshelf
本方式特点:代码简单,开源组件,Windows服务可运行多个实例
Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com
第1步:引用程序集TopShelf.dll和log4net.dll
第2步:创建一个服务类MyClass,里面包含两个方法Start和Stop,还包含一个定时器Timer,每隔5秒往文本文件中写入字符
public class MyClass readonly Timer _timer; private static readonly string FileName = Directory.GetCurrentDirectory ( ) + @"\" + "test.txt"; public MyClass ( ) _timer = new Timer ( 5000 ) AutoReset = true , Enabled = true _timer.Elapsed += delegate ( object sender , ElapsedEventArgs e ) this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) ); void witre ( string context ) StreamWriter sw = File.AppendText ( FileName ); sw.WriteLine ( context ); sw.Flush ( ); sw.Close ( ); public void Start ( ) this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) ); public void Stop ( ) this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine ); }
第3步:使用Topshelf宿主我们的服务,主要是Topshelf如何设置我们的服务的配置和启动和停止的时候的方法调用
class Program { static void Main ( string [ ] args ) { HostFactory.Run ( x => { x.Service ( ( s ) => { s.SetServiceName ( "ser" ); s.ConstructUsing ( name => new MyClass ( ) ); s.WhenStarted ( ( t ) => t.Start ( ) ); s.WhenStopped ( ( t ) => t.Stop ( ) ); } ); x.RunAsLocalSystem ( ); //服务的描述 x.SetDescription ( "Topshelf_Description" ); //服务的显示名称 x.SetDisplayName ( "Topshelf_DisplayName" ); //服务名称 x.SetServiceName ( "Topshelf_ServiceName" ); } ); } }
第4步: cmd命令
ConsoleApp_Topshelf.exe install (安装Windows服务)
ConsoleApp_Topshelf.exe uninstall (卸载Windows服务)
代码下载:http://files.cnblogs.com/aierong/ConsoleApp_Topshelf.rar
c.利用小工具instsrv和srvany
本方式特点:代码超级简单,WindowsForm程序即可,并支持程序交互(本人最喜欢的特点),好像不支持win7,支持xp win2003
首先介绍2个小工具:
instsrv.exe:用以安装和卸载可执行的服务
srvany.exe:用于将任何EXE程序作为Windows服务运行
这2个工具都是是Microsoft Windows Resource Kits工具集的实用的小工具
你可以通过下载并安装Microsoft Windows Resource Kits获得 http://www.microsoft.com/en-us/download/details.aspx?id=17657
第1步: 新建WindowsForm程序
public partial class Form1 : Form Timer _timer; private static readonly string FileName = Application.StartupPath + @"\" + "test.txt"; public Form1 ( ) InitializeComponent ( ); private void Form1_Load ( object sender , EventArgs e ) _timer = new Timer ( ) Enabled = true , Interval = 5000 _timer.Tick += delegate ( object _sender , EventArgs _e ) this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) ); void _timer_Tick ( object sender , EventArgs e ) throw new NotImplementedException ( ); void witre ( string context ) StreamWriter sw = File.AppendText ( FileName ); sw.WriteLine ( context ); sw.Flush ( ); sw.Close ( ); private void button1_Click ( object sender , EventArgs e ) MessageBox.Show ( "Hello" ); }
第2步:安装,卸载
服务的安装步骤分5小步:
(1)打开CMD,输入以下内容,其中WindowsForms_WindowsService为你要创建的服务名称
格式:目录绝对路径\instsrv WindowsForms_WindowsService 目录绝对路径\srvany.exe
例如:
D:\TempWork\win\Debug\instsrv.exe WindowsForms_WindowsService D:\TempWork\win\Debug\srvany.exe
(2)regedit打开注册表编辑器,找到以下目录
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WindowsForms_WindowsService
(3)鼠标右键单击WindowsForms_WindowsService,创建一个"项",名称为"Parameters"
(4)鼠标左键单击"Parameters",在右边点击鼠标右键,创建一个"字符串值"(REG_SZ),名称为"Application",数值数据里填写目录下可执行文件的绝对路径+文件名
例如:
D:\TempWork\win\Debug\WindowsFormsApplication_Exe.exe
(5)打开services.msc服务控制面板,找到WindowsForms_WindowsService服务,鼠标右键-属性-登陆,勾选"允许服务与桌面交互"
启动服务,可以看到程序界面
卸载服务
D:\TempWork\win\Debug\instsrv.exe WindowsForms_WindowsService REMOVE
代码下载:http://files.cnblogs.com/aierong/WindowsFormsApplication_Exe.rar
创建,Windows,服务,的,几种,方式,总结,最近,windows10蓝牙驱动的安装的步骤教程
1、以华硕电脑为例,Win键+R 键打开运行窗口,输入【dxdiag】并确定,启动DirectX诊断工具。
2、【系统】--【系统信息】下可以查看到笔记本的型号。
3、进入华硕笔记本的中文官网,点击【服务与支持 】菜单。
4、搜索笔记本相应的型号,如K42JZ。
5、网站找到相应的笔记本型号的详细信息,在【驱动程序和工具软件】下选择操作系统的位数。
6、在驱动列表中找到蓝牙,打开下拉菜单,点击【中国】下载。
7、下载完成后,解压,双击setup.exe程序根据安装向导提示安装。
以上就是windows10蓝牙驱动的安装的步骤教程啦,希望能帮助到大家。
WindowsXP提速——让你的XP飞起来
第一步、开机滚动条加速你有没有发现当你启动Windows XP系统下载,那个蓝色的滚动条都要滚动很多次呢,其实我们完全可以通过注册表将滚动时间减少,从而加快打款速度。
首先将注册表编辑器打开找到HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerMemory ManagementPrefetchParameters,在右边找到EnablePrefetcher的主键,并将它的默认值3改为1,然后你就会发现滚动的时间大大减少了。
第二步、开机加速在XP系统中关机时,系统会发送相关消息到运行程序和远程服务器,以此通告它们系统将要关闭,并且等待回应后才开始关机。
想要加快开机速度,你可以设置自动结束任务,我们先找到HKEY_CURRENT_USERControl PanelDesktop,把AutoEndTasks的键值设置为1;然后在该分支下有个 HungAppTimeout ,把它的值改为 4000(或更少),默认为50000;最后再找到HKEY_LOCAL_MACHINE www.51xp.cc SystemCurrentControlSetControl,同样把WaitToKillServiceTimeout设置为 4000 ;经过一番设置关机速度将会减少不少。
第三步、宽带加速专业版的Windows XP系统下载保留了20%的带宽,这是系统默认的,然而这对于我们普通用户来是没有什么作用的。
以其让它闲着不如充分将其利用起来。
首先可以在 开始 运行 中输入gpedit.msc,打开组策略编辑器。
找到 计算机配置 管理模板 网络 QoS数据包调度程序 ,选择右边的 限制可保留带宽 ,选择 属性 打开限制可保留带宽属性对话框,选择 禁用 即可。
这样就释放了保留的带宽,就这样,宽带加速了 网速也提高了,相信你一定会刚到很满意的。
WindowsXP,提速,—,让,你的,飞,起来,虽然,如今