網站首頁 編程語言 正文
需求
針對一種特殊的應用, 不需要顯示GUI, 希望常駐在Windows服務當中,在必要的時候我們可以進行啟動或開機啟動。
這個時候我們就可以創建WindowsService 來實現。
創建WindowsService
下面演示了使用VisualStudio2019創建一個基于.NetFramework的Windows服務
項目結構如下所示:
包含了一個啟動項以及一個服務類
右鍵查看 Service1代碼, 如下所示, 包含了重寫OnStart方法以及OnStop方法:
public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } }
當服務被啟動, 即啟動OnStart方法內執行的代碼, 而在ServiceBase當中, 同樣提供了多種類型的方法被重寫。
當我們寫完了該服務的執行代碼之后, 下一步我們要為其添加一個安裝程序。
雙擊Service1.cs, 然后右鍵添加安裝程序,如下所示:
此時, 項目結構當中新增了一個默認名:ProjectInstaller.cs類, 而對應的設計頁面如下所示:
serviceProcessInstaller1:
查看該類的屬性,如下所示:
說明:
Account: 默認設置為User, 當 Account 屬性為時 User , Username 和 Password 屬性用于定義用于運行服務應用程序的帳戶。
Username和 Password 對允許服務在除系統帳戶之外的其他帳戶下運行。 例如,如果沒有用戶登錄,則可以允許服務在重新啟動時自動啟動。 如果保留 Username 或 Password 為空,并且將設置 Account 為 User ,則在安裝時系統將提示您輸入有效的用戶名和密碼。
還可以指定服務在本地系統帳戶下運行,或以本地或網絡服務運行。 ServiceAccount有關帳戶類型的詳細信息,請參閱枚舉:
serviceInstaller1:
查看該類的屬性,如下所示:
注: 該類擴展 ServiceBase 來實現服務。 在安裝服務應用程序時由安裝實用工具調用該類。
說明:
- DelayedAutoStart : 若要延遲該服務的自動啟動,則為 true;否則為 false。 默認值為 false。
- Description : 服務的說明。 默認值為空字符串("")。
- DisplayName : 與服務關聯的名稱,常用于交互工具。
- ServiceName: 要安裝的服務的名稱。 該值必須在安裝實用工具嘗試安裝服務以前進行設置。
- ServicesDependedOn : 在與該安裝程序關聯的服務運行以前必須運行的一組服務。
- StartType : 表示服務的啟動方式。 默認值為 Manual,指定在計算機重新啟動后服務將不會自動啟動。
控制WindowsService
創建完成服務之后, 接下來就是針對服務進行控制, 現在,可以使用 ServiceController 類來連接和控制現有服務的行為。
ServiceController: 表示 Windows 服務并允許連接到正在運行或者已停止的服務、對其進行操作或獲取有關它的信息。
通過ServiceController,我們可以獲取本機的Service服務,以及啟動、暫停、延續、掛起、關閉、刷新等動作, 如下所示:
下面的示例演示如何使用 ServiceController 類來控制 Service1 服務示例。
using System; using System.ServiceProcess; using System.Diagnostics; using System.Threading; namespace ServiceControllerSample { class Program { public enum SimpleServiceCustomCommands { StopWorker = 128, RestartWorker, CheckWorker }; static void Main(string[] args) { ServiceController[] scServices; scServices = ServiceController.GetServices(); foreach (ServiceController scTemp in scServices) { if (scTemp.ServiceName == "Service1") { // Display properties for the Simple Service sample // from the ServiceBase example. ServiceController sc = new ServiceController("Simple Service"); Console.WriteLine("Status = " + sc.Status); Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue); Console.WriteLine("Can ShutDown = " + sc.CanShutdown); Console.WriteLine("Can Stop = " + sc.CanStop); if (sc.Status == ServiceControllerStatus.Stopped) { sc.Start(); while (sc.Status == ServiceControllerStatus.Stopped) { Thread.Sleep(1000); sc.Refresh(); } } // Issue custom commands to the service // enum SimpleServiceCustomCommands // { StopWorker = 128, RestartWorker, CheckWorker }; sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker); sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker); sc.Pause(); while (sc.Status != ServiceControllerStatus.Paused) { Thread.Sleep(1000); sc.Refresh(); } Console.WriteLine("Status = " + sc.Status); sc.Continue(); while (sc.Status == ServiceControllerStatus.Paused) { Thread.Sleep(1000); sc.Refresh(); } Console.WriteLine("Status = " + sc.Status); sc.Stop(); while (sc.Status != ServiceControllerStatus.Stopped) { Thread.Sleep(1000); sc.Refresh(); } Console.WriteLine("Status = " + sc.Status); String[] argArray = new string[] { "ServiceController arg1", "ServiceController arg2" }; sc.Start(argArray); while (sc.Status == ServiceControllerStatus.Stopped) { Thread.Sleep(1000); sc.Refresh(); } Console.WriteLine("Status = " + sc.Status); // Display the event log entries for the custom commands // and the start arguments. EventLog el = new EventLog("Application"); EventLogEntryCollection elec = el.Entries; foreach (EventLogEntry ele in elec) { if (ele.Source.IndexOf("Service1.OnCustomCommand") >= 0 | ele.Source.IndexOf("Service1.Arguments") >= 0) Console.WriteLine(ele.Message); } } } } } } //This sample displays the following output if the Simple Service //sample is running: //Status = Running //Can Pause and Continue = True //Can ShutDown = True //Can Stop = True //Status = Paused //Status = Running //Status = Stopped //Status = Running //4:14:49 PM - Custom command received: 128 //4:14:49 PM - Custom command received: 129 //ServiceController arg1 //ServiceController arg2
安裝WindowsService
能夠控制我們創建的服務的前提是, 該服務已安裝在我們調試的設備上, 我們可以通過AssemblyInstaller 類來進行安裝。
安裝示例
在下面的示例中, AssemblyInstaller 通過調用 AssemblyInstaller 構造函數來創建。 設置此對象的屬性,并 Install Commit 調用和方法以安裝 MyAssembly.exe 程序集。
using System; using System.Configuration.Install; using System.Collections; using System.Collections.Specialized; class AssemblyInstaller_Example { static void Main() { IDictionary mySavedState = new Hashtable(); Console.WriteLine( "" ); try { // Set the commandline argument array for 'logfile'. string[] commandLineOptions = new string[ 1 ] {"/LogFile=example.log"}; // Create an object of the 'AssemblyInstaller' class. AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller( "MyAssembly.exe" , commandLineOptions ); myAssemblyInstaller.UseNewContext = true; // Install the 'MyAssembly' assembly. myAssemblyInstaller.Install( mySavedState ); // Commit the 'MyAssembly' assembly. myAssemblyInstaller.Commit( mySavedState ); } catch (Exception e) { Console.WriteLine( e.Message ); } } }
卸載示例
下面的示例演示的 Uninstall 方法 Installer 。 Uninstall方法在的派生類中被重寫 Installer 。
// Override 'Uninstall' method of Installer class. public override void Uninstall( IDictionary mySavedState ) { if (mySavedState == null) { Console.WriteLine("Uninstallation Error !"); } else { base.Uninstall( mySavedState ); Console.WriteLine( "The Uninstall method of 'MyInstallerSample' has been called" ); } }
原文鏈接:https://www.cnblogs.com/zh7791/p/14293741.html
相關推薦
- 2022-02-18 cv2.error: OpenCV(4.5.4-dev) :-1: error: (-5:Bad a
- 2022-08-01 Python?OpenCV基于HSV的顏色分割實現示例_python
- 2022-05-20 Python?動態綁定屬性和方法?_python
- 2022-09-14 前端使用svg圖片改色實現示例_其它綜合
- 2022-03-30 python批量修改xml文件中的信息_python
- 2022-03-04 如何在uni-app中選擇一個合適的UI組件庫
- 2022-05-22 關于VS2022不能使用<bits/stdc++.h>的解決方案(萬能頭文件)_C 語言
- 2022-12-19 Python?完美解決?Import?“模塊“?could?not?be?resolved?...的
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支