程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#實現Windows 服務的制作安裝和刪除

C#實現Windows 服務的制作安裝和刪除

編輯:關於.NET

今天為大家整理一篇關於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服務裡啟動你安裝的服務就可以了。

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved