最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#創(chuàng)建控制Windows服務(wù)

 更新時間:2022年02月07日 11:51:16   作者:痕跡g  
這篇文章介紹了C#創(chuàng)建和控制Windows服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

需求

針對一種特殊的應(yīng)用, 不需要顯示GUI, 希望常駐在Windows服務(wù)當(dāng)中,在必要的時候我們可以進(jìn)行啟動或開機啟動。

這個時候我們就可以創(chuàng)建WindowsService 來實現(xiàn)。

創(chuàng)建WindowsService

下面演示了使用VisualStudio2019創(chuàng)建一個基于.NetFramework的Windows服務(wù)

項目結(jié)構(gòu)如下所示:

包含了一個啟動項以及一個服務(wù)類

右鍵查看 Service1代碼, 如下所示, 包含了重寫OnStart方法以及OnStop方法:

     public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }

當(dāng)服務(wù)被啟動, 即啟動OnStart方法內(nèi)執(zhí)行的代碼, 而在ServiceBase當(dāng)中, 同樣提供了多種類型的方法被重寫。

當(dāng)我們寫完了該服務(wù)的執(zhí)行代碼之后, 下一步我們要為其添加一個安裝程序。

雙擊Service1.cs, 然后右鍵添加安裝程序,如下所示:

此時, 項目結(jié)構(gòu)當(dāng)中新增了一個默認(rèn)名:ProjectInstaller.cs類, 而對應(yīng)的設(shè)計頁面如下所示:

serviceProcessInstaller1:

查看該類的屬性,如下所示:

說明:

Account: 默認(rèn)設(shè)置為User, 當(dāng) Account 屬性為時 User , Username 和 Password 屬性用于定義用于運行服務(wù)應(yīng)用程序的帳戶。

Username和 Password 對允許服務(wù)在除系統(tǒng)帳戶之外的其他帳戶下運行。 例如,如果沒有用戶登錄,則可以允許服務(wù)在重新啟動時自動啟動。 如果保留 Username 或 Password 為空,并且將設(shè)置 Account 為 User ,則在安裝時系統(tǒng)將提示您輸入有效的用戶名和密碼。

還可以指定服務(wù)在本地系統(tǒng)帳戶下運行,或以本地或網(wǎng)絡(luò)服務(wù)運行。 ServiceAccount有關(guān)帳戶類型的詳細(xì)信息,請參閱枚舉:

serviceInstaller1:

查看該類的屬性,如下所示:

注: 該類擴展 ServiceBase 來實現(xiàn)服務(wù)。 在安裝服務(wù)應(yīng)用程序時由安裝實用工具調(diào)用該類。

說明:

  • DelayedAutoStart : 若要延遲該服務(wù)的自動啟動,則為 true;否則為 false。 默認(rèn)值為 false。
  • Description : 服務(wù)的說明。 默認(rèn)值為空字符串("")。
  • DisplayName : 與服務(wù)關(guān)聯(lián)的名稱,常用于交互工具。
  • ServiceName: 要安裝的服務(wù)的名稱。 該值必須在安裝實用工具嘗試安裝服務(wù)以前進(jìn)行設(shè)置。
  • ServicesDependedOn : 在與該安裝程序關(guān)聯(lián)的服務(wù)運行以前必須運行的一組服務(wù)。
  • StartType : 表示服務(wù)的啟動方式。 默認(rèn)值為 Manual,指定在計算機重新啟動后服務(wù)將不會自動啟動。

控制WindowsService

創(chuàng)建完成服務(wù)之后, 接下來就是針對服務(wù)進(jìn)行控制, 現(xiàn)在,可以使用 ServiceController 類來連接和控制現(xiàn)有服務(wù)的行為。

ServiceController: 表示 Windows 服務(wù)并允許連接到正在運行或者已停止的服務(wù)、對其進(jìn)行操作或獲取有關(guān)它的信息。

通過ServiceController,我們可以獲取本機的Service服務(wù),以及啟動、暫停、延續(xù)、掛起、關(guān)閉、刷新等動作, 如下所示:

下面的示例演示如何使用 ServiceController 類來控制 Service1 服務(wù)示例。

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

能夠控制我們創(chuàng)建的服務(wù)的前提是, 該服務(wù)已安裝在我們調(diào)試的設(shè)備上, 我們可以通過AssemblyInstaller 類來進(jìn)行安裝。

安裝示例

在下面的示例中, AssemblyInstaller 通過調(diào)用 AssemblyInstaller 構(gòu)造函數(shù)來創(chuàng)建。 設(shè)置此對象的屬性,并 Install Commit 調(diào)用和方法以安裝 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" );
   }
}

到此這篇關(guān)于C#創(chuàng)建控制Windows服務(wù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c# 如何用lock解決緩存擊穿

    c# 如何用lock解決緩存擊穿

    這篇文章主要介紹了c# 如何用lock解決緩存擊穿,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-02-02
  • 解析C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范

    解析C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范

    這篇文章主要介紹了C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范,這里我們根據(jù)C#語言的開發(fā)方微軟給出的約定來作為編寫樣式參照,需要的朋友可以參考下
    2016-01-01
  • C#實現(xiàn)字體旋轉(zhuǎn)的方法

    C#實現(xiàn)字體旋轉(zhuǎn)的方法

    這篇文章主要介紹了C#實現(xiàn)字體旋轉(zhuǎn)的方法,涉及C#通過Matrix實現(xiàn)字體旋轉(zhuǎn)效果的方法,需要的朋友可以參考下
    2015-06-06
  • C#中的問號(?號)用法小結(jié)

    C#中的問號(?號)用法小結(jié)

    這篇文章主要介紹了C#中的問號(?號)用法小結(jié),本文介紹了3種用法,分別作為修飾符、運算符的用法,需要的朋友可以參考下
    2014-07-07
  • C#基于SerialPort類實現(xiàn)串口通訊詳解

    C#基于SerialPort類實現(xiàn)串口通訊詳解

    這篇文章主要為大家詳細(xì)介紹了C#基于SerialPort類實現(xiàn)串口通訊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#使用OpenCvSharp實現(xiàn)圖像校正

    C#使用OpenCvSharp實現(xiàn)圖像校正

    這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實現(xiàn)圖像校正功能,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價值,需要的小伙伴可以參考下
    2023-11-11
  • C#中sizeof的用法實例分析

    C#中sizeof的用法實例分析

    這篇文章主要介紹了C#中sizeof的用法,包括了常見的用法及注釋事項,需要的朋友可以參考下
    2014-09-09
  • c#線程間傳遞參數(shù)詳解

    c#線程間傳遞參數(shù)詳解

    本篇文章主要是對c#中的線程間傳遞參數(shù)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#實現(xiàn)語音播報功能的示例詳解

    C#實現(xiàn)語音播報功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用C#實現(xiàn)語音播報功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2024-02-02
  • C#實現(xiàn)發(fā)送手機驗證碼功能

    C#實現(xiàn)發(fā)送手機驗證碼功能

    之前基于c#實現(xiàn)手機發(fā)送驗證碼功能很復(fù)雜,真正做起來也就那回事,不過就是一個post請求就可以實現(xiàn)的東西,今天小編把思路分享到腳本之家平臺,供大家參考下
    2017-06-06

最新評論

盐边县| 化德县| 军事| 长春市| 文安县| 武穴市| 土默特左旗| 大竹县| 红桥区| 泰顺县| 泸定县| 宽城| 准格尔旗| 常山县| 松溪县| 株洲县| 汉源县| 上高县| 桂阳县| 瑞丽市| 澄城县| 辛集市| 图们市| 民县| 辰溪县| 沅江市| 中山市| 宁明县| 眉山市| 奉节县| 安陆市| 襄城县| 长泰县| 陈巴尔虎旗| 淮北市| 宁德市| 广汉市| 新和县| 开平市| 海兴县| 曲麻莱县|