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. 右击设计视图选择“添加安装程序”
from:http://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://www.cnblogs.com/chiropter/articles/1932463.html