博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#写windows 服务
阅读量:2286 次
发布时间:2019-05-09

本文共 4204 字,大约阅读时间需要 14 分钟。

c#
windows
服务
,
我想网上到处都是
,
我就不多说了如果你要在服务中调用一个
.exe
的话
!
using System.Diagnostics;using System.ServiceProcess;private Process NEC_process = null;在onstart(){  System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();                Info.FileName ="";//.exe的路径                Info.WorkingDirectory = "";//工作目录                Info.WindowStyle = ProcessWindowStyle.Minimized;//exe是否显示程序窗口                NEC_process = System.Diagnostics.Process.Start(Info);                 NEC_process.Start();//开启一个进程}onstop(){     NEC_process.Kill(); }

监控系统进程   
监测到别的应用程序是否启动或关闭  

Process   []     process   =   Process.GetProcessesByName(   "你的程序在任务管理器中的名称"   );           if(process.Length>1)           {             MessageBox.Show("已启动了一个此程序");             Application.ExitThread();               }

程序代码

using System;using System.Collections;using System.ComponentModel;using System.Diagnostics;using System.ServiceProcess;using System.Configuration.Install;namespace CjjerTest.DotNet.ServiceDST{    // 应用程序    public class MyFirstService : System.ServiceProcess.ServiceBase    {        public MyFirstService()        {            this.CanPauseAndContinue = true;            this.CanShutdown = true;            this.CanHandleSessionChangeEvent = false;            this.ServiceName = "MyFirstService";        }        protected override void OnStart(string[] args){        }        protected override void OnStop(){        }        protected override void OnContinue(){        }        //启动        public static void Main()        {            ServiceBase[] servicesToRun = new ServiceBase[] {new MyFirstService()};            ServiceBase.Run(servicesToRun);        }    }    //安装    [RunInstaller(true)]    public class ProjectInstaller : System.Configuration.Install.Installer    {        private ServiceProcessInstaller myServiceProcessInstaller;        private ServiceInstaller myServiceInstaller;        public ProjectInstaller()        {            this.myServiceProcessInstaller = new ServiceProcessInstaller();            this.myServiceInstaller = new ServiceInstaller();            // 安装            // 用户名 和 密码            this.myServiceProcessInstaller.Account = ServiceAccount.LocalSystem;            this.myServiceProcessInstaller.Username = null;            this.myServiceProcessInstaller.Password = null;            // 服务名称,这样可以在net stop XX 里面使用了            // 启动类型            this.myServiceInstaller.ServiceName = "MyFirstService";            this.myServiceInstaller.StartType = ServiceStartMode.Automatic;            // 加入            this.Installers.AddRange(new Installer[] {this.myServiceProcessInstaller, this.myServiceInstaller});        }    }}  protected override void OnStart(string[] args){                StreamWriter sw=new StreamWriter(@"E:\web\web\net\test\sernet.txt",true);            sw.Write("\r\n另一条数据");            sw.Close();            base.OnStart( args );        }

今天用c#windows服务程序,写啊写啊,查了资料,网上有篇文章写得不错

在很多应用中需要做windows服务来操作数据库等操作,比如
1)一些非常慢的数据库操作,不想一次性去做,想慢慢的通过服务定时去做,比如定时为数据库备份等
2)在.net Remoting中利用windows服务来做Host

利用vs.net我们可以在几分钟之内建立其windows服务,非常简单

下面说一下步骤

1. 新建一个项目
2.
从一个可用的项目模板列表当中选择Windows服务
3.
设计器会以设计模式打开
4.
从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意:要确保是从组件列表而不是从Windows窗体列表当中使用Timer)  
5.
设置Timer属性,Interval属性200毫秒(1秒进行5次数据库操作)
6.
然后为这个服务填加功能
7.
双击这个Timer,然后在里面写一些数据库操作的代码,比如
SqlConnection conn=new SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=275280");
     SqlCommand comm=-new SqlCommand("insert into tb1 ('111',11)",conn);
     conn.Open();
     comm.ExecuteNonQuery();
     conn.Close();
8.
将这个服务程序切换到设计视图
9.
右击设计视图选择添加安装程序

fromhttp://post.blog.hexun.com/imagine18/trackback.aspx?articleid=6056177

10. 切换到刚被添加的ProjectInstaller的设计视图
11.
设置serviceInstaller1组件的属性:  
      1) ServiceName = My Sample Service
      2) StartType = Automatic (
开机自动运行)
12.
设置serviceProcessInstaller1组件的属性    Account = LocalSystem
13.
改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
14.
执行命令“InstallUtil MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。(InstallUtil这个程序在WINDOWS文件夹\Microsoft.NET\Framework\v1.1.4322下面)
15.
右击桌面上我的电脑,选择管理就可以打计算机管理控制台
16.
服务和应用程序里面的服务部分里,你可以发现你的Windows服务已经包含在服务列表当中了
17.
右击你的服务选择启动就可以启动你的服务了
看看数据库是不是一秒多了5个记录啊
(InstallUtil/u MyWindowsService.exe
删除服务)

转自:

 

 

转载地址:http://quunb.baihongyu.com/

你可能感兴趣的文章
字典转模型的例子
查看>>
UIAlertView的基本使用和对话框中按钮的事件处理方法
查看>>
常用结构体
查看>>
基本数据类型的包装类
查看>>
NSArray数组(1)
查看>>
NSArray数组(2)
查看>>
NSDictionary 字典类
查看>>
NSSet 集合
查看>>
集合之间相互转换
查看>>
集合的内存管理
查看>>
文件操作
查看>>
NSData
查看>>
日期操作
查看>>
iOS的三种弹框
查看>>
UIApplication和程序启动过程
查看>>
cocoaPods安装2017 以及遇到的坑
查看>>
Android中自定义可以选择中文的NumberPicker屏蔽弹出软键盘
查看>>
Struts2+JQuery.uploadify插件实现带进度的多文件上传示例【也可以设置去掉进度条的显示】
查看>>
基于jQuery的AJAX和JSON的实例[另附文章:深入浅出json]
查看>>
jquery.validate的ajax方式验证[可以一个控件下一次传递多个参数,已经成功通过验证]
查看>>