当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C#实现Windows 服务的制作安装和删除

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

ASP.NET 中的 C#实现Windows 服务的制作安装和删除


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 257 ::
收藏到网摘: n/a

  今天为大家整理一篇关于C#实现windows服务的制作与安装还有删除的文章,希望能帮助学习C#的同学进一步提高学习水平。

  运行Visual Studio.NET,建立一个C#的Windows服务项目。

  主程序代码:

以下为引用的内容:
以下是引用片段:
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.ServiceProcess;
  using System.Threading;
  using System.Windows.Forms;
  namespace CareEye
  ...{
  public class CareEye : System.ServiceProcess.ServiceBase
  ...{
  private Thread MainThread;
  /**//// 
  /// 必需的设计器变量。
  /// 
  private System.ComponentModel.Container components = null;
  public CareEye()
  ...{
  // 该调用是 Windows.Forms 组件设计器所必需的。
  InitializeComponent();
  // TODO: 在 InitComponent 调用后添加任何初始化
  MainThread = new Thread(new ThreadStart(ThreadFunc));
  MainThread.Priority = ThreadPriority.Lowest;
  }
  // 进程的主入口点
  static void Main()
  ...{
  //System.ServiceProcess.ServiceBase[] ServicesToRun;
  // 同一进程中可以运行多个用户服务。若要将
  //另一个服务添加到此进程,请更改下行
  // 以创建另一个服务对象。例如,
  //
  // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new CareEye(), new MySecondUserService()};
  //
  //ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CareEye() };
  System.ServiceProcess.ServiceBase.Run(new CareEye());
  }
  /**//// 
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// 
  private void InitializeComponent()

以下为引用的内容:
  ...{
  //
  // CareEye
  //
  this.ServiceName = "CareEye";
  }
  /**//// 
  /// 清理所有正在使用的资源。
  /// 
  protected override void Dispose(bool disposing)
  ...{
  if (disposing)
  ...{
  if (components != null)
  ...{
  components.Dispose();
  }
  }
  base.Dispose(disposing);
  }
  /**//// 
  /// 设置具体的操作,以便服务可以执行它的工作。
  /// 
  protected override void OnStart(string[] args)
  ...{
  // TODO: 在此处添加代码以启动服务。
  MainThread.Start();
  }
  /**//// 
  /// 停止此服务。
  /// 
  protected override void OnStop()
  ...{
  // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
  MainThread.Abort();
  }
  public static void ThreadFunc()
  ...{
  int LastHour = DateTime.Now.Hour;
  while (true)
  ...{
  System.Threading.Thread.Sleep(60000);
  if (DateTime.Now.Hour - 1 == LastHour)
  ...{
  MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
  LastHour = DateTime.Now.Hour;
  }
  }
  }
  }
  }

  添加安装组件:

  在设计页面上点右键,出现菜单后,选择添加安装程序。这时会出现一个新的页面,页面上有个控件 serviceProcessInstaller1和serviceInstaller1

  在 serviceProcessInstaller1中把属性Account改为LocalSystem

  在把serviceInstaller1中把属性Parent 改为serviceProcessInstaller1 ServiceName属性是管生成服务后的名子

  添加完成之后,生成一下(假设名为W2.exe)。到相应的文件夹找到生成的exe文件,找到时会发现有两个.exe用名子比较短的那个。把这个文件拷到一个好记的文件夹中如F盘根目录。

  这时就是要把个服务安装一下。进入cmd中的画面,进入Framework2.0的文件如:

  cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

  后在打

  InstallUtil f:\w2.exe 这个就安装了服务 卸载服务是 InstallUtil f:\w2.exe -u

  现在就剩启动服务了,

  到windows服务里启动你安装的服务就可以了。