不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
以编程方式编写服务
编辑:dnawo 日期:2008-09-13
1.打开 Microsoft Visual Studio 2005 创建一个空项目;
2.添加对System、System.ServiceProcess、System.Configuration.Install的引用;
3.新建一个Windows服务文件Service1.cs,代码如下:
参考文章:
1.《如何:以编程方式编写服务》:http://msdn.microsoft.com/zh-cn/76477d2t(VS.80).aspx
2.ServiceProcessInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceprocessinstaller(VS.80).aspx
3.ServiceInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceinstaller(VS.80).aspx
2.添加对System、System.ServiceProcess、System.Configuration.Install的引用;
3.新建一个Windows服务文件Service1.cs,代码如下:
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;
namespace WindowsService2
{
public class Service1 : ServiceBase
{
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 应用程序的主入口点
/// </summary>
public static void Main()
{
ServiceBase.Run(new Service1());
}
/// <summary>
/// 实例化对象
/// </summary>
public Service1()
{
this.ServiceName = "NewService";
this.CanStop = true;
this.CanPauseAndContinue = true;
this.AutoLog = true;
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
if (!File.Exists(@"C:\ServiceLog.log"))
{
File.Create(@"C:\ServiceLog.log");
}
File.AppendAllText(@"C:\ServiceLog.log", string.Format("启动时间:{0}\r\n", DateTime.Now.ToString()));
//设置一个定时器
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 3000;//间隔时间
timer1.Elapsed += new System.Timers.ElapsedEventHandler(Done);//达到间隔时间执行的处理程序
timer1.AutoReset = true;//每次间隔结束时引发 System.Timers.Timer.Elapsed 事件,如果仅在第一次引发设置为false
timer1.Enabled = true;
}
/// <summary>
/// 停止服务
/// </summary>
protected override void OnStop()
{
File.AppendAllText(@"C:\ServiceLog.log", string.Format("停止时间:{0}\r\n", DateTime.Now.ToString()));
}
/// <summary>
/// 定时任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Done(object sender, EventArgs e)
{
//这边可以做些其他操作
File.AppendAllText(@"C:\ServiceLog.log", string.Format("执行定时任务:{0}\r\n", DateTime.Now.ToString()));
}
/// <summary>
/// 释放
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}
//[RunInstaller(true)]
[RunInstallerAttribute(true)]
public class ProjectInstaller : Installer
{
private System.ComponentModel.IContainer components = null;
ServiceInstaller serviceInstaller1;
ServiceProcessInstaller serviceProcessInstaller1;
public ProjectInstaller()
{
this.serviceInstaller1 = new ServiceInstaller();
this.serviceProcessInstaller1 = new ServiceProcessInstaller();
this.serviceInstaller1.ServiceName = "NewService";
this.serviceInstaller1.StartType = ServiceStartMode.Automatic;
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
this.Installers.Add(serviceInstaller1);
this.Installers.Add(serviceProcessInstaller1);
}
/// <summary>
/// 释放
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;
namespace WindowsService2
{
public class Service1 : ServiceBase
{
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 应用程序的主入口点
/// </summary>
public static void Main()
{
ServiceBase.Run(new Service1());
}
/// <summary>
/// 实例化对象
/// </summary>
public Service1()
{
this.ServiceName = "NewService";
this.CanStop = true;
this.CanPauseAndContinue = true;
this.AutoLog = true;
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
if (!File.Exists(@"C:\ServiceLog.log"))
{
File.Create(@"C:\ServiceLog.log");
}
File.AppendAllText(@"C:\ServiceLog.log", string.Format("启动时间:{0}\r\n", DateTime.Now.ToString()));
//设置一个定时器
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 3000;//间隔时间
timer1.Elapsed += new System.Timers.ElapsedEventHandler(Done);//达到间隔时间执行的处理程序
timer1.AutoReset = true;//每次间隔结束时引发 System.Timers.Timer.Elapsed 事件,如果仅在第一次引发设置为false
timer1.Enabled = true;
}
/// <summary>
/// 停止服务
/// </summary>
protected override void OnStop()
{
File.AppendAllText(@"C:\ServiceLog.log", string.Format("停止时间:{0}\r\n", DateTime.Now.ToString()));
}
/// <summary>
/// 定时任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Done(object sender, EventArgs e)
{
//这边可以做些其他操作
File.AppendAllText(@"C:\ServiceLog.log", string.Format("执行定时任务:{0}\r\n", DateTime.Now.ToString()));
}
/// <summary>
/// 释放
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}
//[RunInstaller(true)]
[RunInstallerAttribute(true)]
public class ProjectInstaller : Installer
{
private System.ComponentModel.IContainer components = null;
ServiceInstaller serviceInstaller1;
ServiceProcessInstaller serviceProcessInstaller1;
public ProjectInstaller()
{
this.serviceInstaller1 = new ServiceInstaller();
this.serviceProcessInstaller1 = new ServiceProcessInstaller();
this.serviceInstaller1.ServiceName = "NewService";
this.serviceInstaller1.StartType = ServiceStartMode.Automatic;
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
this.Installers.Add(serviceInstaller1);
this.Installers.Add(serviceProcessInstaller1);
}
/// <summary>
/// 释放
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
参考文章:
1.《如何:以编程方式编写服务》:http://msdn.microsoft.com/zh-cn/76477d2t(VS.80).aspx
2.ServiceProcessInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceprocessinstaller(VS.80).aspx
3.ServiceInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceinstaller(VS.80).aspx
评论: 0 | 引用: 0 | 查看次数: 3909
发表评论
请登录后再发表评论!