夜隼

RYSZ

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、新建服务项目

比如

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 CatchArticle
{
 public class CatchArticle : System.ServiceProcess.ServiceBase
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private Thread MainThread;
  public CatchArticle()
  {
   // 该调用是 Windows.Forms 组件设计器所必需的。
   InitializeComponent();

   MainThread=new Thread(new ThreadStart(ThreadFunc));
    MainThread.Priority=ThreadPriority.Lowest;
  }

  // 进程的主入口点
  static void Main()
  {
   System.ServiceProcess.ServiceBase[] ServicesToRun;
   ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CatchArticle() };
   System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  }

  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   components = new System.ComponentModel.Container();
   this.ServiceName = "CatchArticle";
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  /// <summary>
  /// 设置具体的操作,以便服务可以执行它的工作。
  /// </summary>
  protected override void OnStart(string[] args)
  {
   MainThread.Start();
  }
 
  /// <summary>
  /// 停止此服务。
  /// </summary>
  protected override void OnStop()
  {
   MainThread.Abort();
  }

  public static void ThreadFunc()
   {
     int LastHour=DateTime.Now.Hour;
     while (true)
     {
      System.Threading.Thread.Sleep(1000);
      if (DateTime.Now.Hour-1==LastHour)
      {
        MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
     LastHour=DateTime.Now.Hour;
      }
     }
   }

 }
}

 

========================================

2、然后增加一个ProjectInstall.cs 继承自Installer ,里面的一些参数可以自己定义

using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;

namespace CatchArticle
{
 /// <summary>
 /// myInstall 的摘要说明。
 /// </summary>
 ///
 [RunInstaller(true)]
 public class myInstall : Installer
 {

  private ServiceInstaller serviceInstaller;
  private ServiceProcessInstaller processInstaller;
  public myInstall()
  {
   processInstaller = new ServiceProcessInstaller();
   serviceInstaller = new ServiceInstaller();

   processInstaller.Account = ServiceAccount.LocalSystem;
   serviceInstaller.StartType = ServiceStartMode.Automatic;
   serviceInstaller.ServiceName = "CatchArticle";

   Installers.Add(serviceInstaller);
   Installers.Add(processInstaller);
  }
 }
}

 

 

3、然后便以Release

4、使用C:\......\Framework\v1.1..\installUtil.exe C:\项目路径\bin\release\TestService.exe

 

注意:如果不增加ProjectInstall.cs,则会报如下错误

正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
   assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
   logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
   assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
   logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
没有安装程序,因此移除 InstallState 文件。
正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
   assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
   logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
受影响的参数是:
   assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
   logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
没有安装程序,因此移除 InstallState 文件。

posted on 2006-06-21 10:14  夜隼  阅读(3136)  评论(1编辑  收藏  举报