公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#守護(hù)進(jìn)程(Windows后臺(tái)服務(wù)程序及前臺(tái)管理程序設(shè)計(jì))

admin
2021年1月29日 16:59 本文熱度 2881

一、創(chuàng)建Windows Service

1、新建一個(gè)Windows Service,并將項(xiàng)目名稱改為“MyWindowsService”,如下圖所示:

2、在解決方案資源管理器內(nèi)將Service1.cs改為MyService1.cs后并點(diǎn)擊“查看代碼”圖標(biāo)按鈕進(jìn)入代碼編輯器界面,如下圖所示:


3、在代碼編輯器內(nèi)如入以下代碼,如下所示:

using System;

using System.ServiceProcess;

using System.IO;

 

namespace MyWindowsService

{

    public partial class MyService : ServiceBase

    {

        public MyService()

        {

            InitializeComponent();

        }

 

        string filePath = @"D:\MyServiceLog.txt";

 

        protected override void OnStart(string[] args)

        {
        //這塊可以啟動(dòng)要守護(hù)的進(jìn)程

            using (FileStream stream = new FileStream(filePath,FileMode.Append))

            using (StreamWriter writer = new StreamWriter(stream))

            {

                writer.WriteLine("{DateTime.Now},服務(wù)啟動(dòng)!");

            }

        }

 

        protected override void OnStop()

        {

            using (FileStream stream = new FileStream(filePath, FileMode.Append))

            using (StreamWriter writer = new StreamWriter(stream))

            {

                writer.WriteLine("{DateTime.Now},服務(wù)停止!");

            }

        }

    }

}


4、雙擊項(xiàng)目“MyWindowsService”進(jìn)入“MyService”設(shè)計(jì)界面,在空白位置右擊鼠標(biāo)彈出上下文菜單,選中“添加安裝程序”,如下圖所示:

5、此時(shí)軟件會(huì)生成兩個(gè)組件,分別為“serviceInstaller1”及“serviceProcessInstaller1”,如下圖所示:


6、點(diǎn)擊“serviceInstaller1”,在“屬性”窗體將ServiceName改為MyService,Description改為我的服務(wù),StartType保持為Manual,如下圖所示:


7、點(diǎn)擊“serviceProcessInstaller1”,在“屬性”窗體將Account改為L(zhǎng)ocalSystem(服務(wù)屬性系統(tǒng)級(jí)別),如下圖所示:


8、鼠標(biāo)右鍵點(diǎn)擊項(xiàng)目“MyWindowsService”,在彈出的上下文菜單中選擇“生成”按鈕,如下圖所示:


9、至此,Windows服務(wù)已經(jīng)創(chuàng)建完畢。

二.安裝和啟動(dòng)和卸載服務(wù)(2種方式)

  1.命令行安裝、啟動(dòng)、和卸載

    (1)在電腦中找到找到cmd.exe,一般在C:\Windows\System32目錄下,然后以管理員身份運(yùn)行

    (2)在電腦系統(tǒng)中找到InstallUtil.exe,一般在C:\Windows\Microsoft.NET\Framework\v4.0.30319下面,DOS框切換到這個(gè)目錄下

    (3)執(zhí)行命令:InstallUtil.exe + C:\Users\zhao\Desktop\111\text\WindowsService1\bin\Debug\WindowsService1.exe             (安裝服務(wù))

    (4)net  start  MyService(服務(wù)名)            (啟動(dòng)服務(wù))

    服務(wù)啟動(dòng)到此結(jié)束,如果要卸載此服務(wù),執(zhí)行命令:sc delete MyService

  2.建立一個(gè)窗體,然后去安裝、啟動(dòng)、停止和卸載服務(wù)

    (1)、在同一個(gè)解決方案里新建一個(gè)Windows Form項(xiàng)目,并命名為WindowsServiceClient,如下圖所示:

      

    (2)、將該項(xiàng)目設(shè)置為啟動(dòng)項(xiàng)目,并在窗體內(nèi)添加四個(gè)按鈕,分別為安裝服務(wù)、啟動(dòng)服務(wù)、停止服務(wù)及卸載服務(wù),如下圖所示:

      

    (3)、按下F7進(jìn)入代碼編輯界面,引用“System.ServiceProcess”及“System.Configuration.Install”,并輸入如下代碼:

using System;

using System.Collections;

using System.Windows.Forms;

using System.ServiceProcess;

using System.Configuration.Install;

 

namespace WindowsServiceClient

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        string serviceFilePath = @"C:\Users\zhao\Desktop\111\text\WindowsService1\bin\Debug\WindowsService1.exe";

        string serviceName = "MyService";

 

        //事件:安裝服務(wù)

        private void button1_Click(object sender, EventArgs e)

        {

            if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);

            this.InstallService(serviceFilePath);

        }

 

        //事件:?jiǎn)?dòng)服務(wù)

        private void button2_Click(object sender, EventArgs e)

        {

            if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);

        }

 

        //事件:停止服務(wù)

        private void button4_Click(object sender, EventArgs e)

        {

            if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);

        }

 

        //事件:卸載服務(wù)

        private void button3_Click(object sender, EventArgs e)

        {

            if (this.IsServiceExisted(serviceName))

            {

                this.ServiceStop(serviceName);

                this.UninstallService(serviceFilePath);

            }

        }

 

        //判斷服務(wù)是否存在

        private bool IsServiceExisted(string serviceName)

        {

            ServiceController[] services = ServiceController.GetServices();

            foreach (ServiceController sc in services)

            {

                if (sc.ServiceName.ToLower() == serviceName.ToLower())

                {

                    return true;

                }

            }

            return false;

        }

 

        //安裝服務(wù)

        private void InstallService(string serviceFilePath)

        {

            using (AssemblyInstaller installer = new AssemblyInstaller())

            {

                installer.UseNewContext = true;

                installer.Path = serviceFilePath;

                IDictionary savedState = new Hashtable();

                installer.Install(savedState);

                installer.Commit(savedState);

            }

        }

 

        //卸載服務(wù)

        private void UninstallService(string serviceFilePath)

        {

            using (AssemblyInstaller installer = new AssemblyInstaller())

            {

                installer.UseNewContext = true;

                installer.Path = serviceFilePath;

                installer.Uninstall(null);

            }

        }

        //啟動(dòng)服務(wù)

        private void ServiceStart(string serviceName)

        {

            using (ServiceController control = new ServiceController(serviceName))

            {

                if (control.Status == ServiceControllerStatus.Stopped)

                {

                    control.Start();

                }

            }

        }

 

        //停止服務(wù)

        private void ServiceStop(string serviceName)

        {

            using (ServiceController control = new ServiceController(serviceName))

            {

                if (control.Status == ServiceControllerStatus.Running)

                {

                    control.Stop();

                }

            }

        }

    }

}

    (4)、為了后續(xù)調(diào)試服務(wù)及安裝卸載服務(wù)的需要,將已生成的MyWindowsService.exe引用到本W(wǎng)indows窗體,如下圖所示:

      

    (5)、由于需要安裝服務(wù),故需要使用UAC中Administrator的權(quán)限,鼠標(biāo)右擊項(xiàng)目“WindowsServiceClient”,在彈出的上下文菜單中選擇“添加”->“新建項(xiàng)”,在彈出的選擇窗體中選擇“應(yīng)用程序清單文件”并單擊確定,如下圖所示:

      

    (6)、打開(kāi)該文件,并將<requestedExecutionLevel level="asInvoker" uiAccess="false" />改為<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />,如下圖所示:    

      

    (7)、使用WIN+R的方式打開(kāi)運(yùn)行窗體,并在窗體內(nèi)輸入services.msc后打開(kāi)服務(wù),如下圖所示:

      

    (8)、點(diǎn)擊窗體內(nèi)的“安裝服務(wù)”按鈕,將會(huì)在服務(wù)中出現(xiàn)MyService,如下圖所示:  

      

    (9)、點(diǎn)擊“運(yùn)行服務(wù)”按鈕,將啟動(dòng)并運(yùn)行服務(wù),如下所示:

      

    (10)、點(diǎn)擊“停止服務(wù)”按鈕,將會(huì)停止運(yùn)行服務(wù),如下圖所示:

      

    (11)、點(diǎn)擊“卸載服務(wù)”按鈕,將會(huì)從服務(wù)中刪除MyService服務(wù)。

    (12)、以上啟動(dòng)及停止服務(wù)將會(huì)寫(xiě)入D:\MyServiceLog.txt,內(nèi)容如下所示:

           

補(bǔ)充:如何調(diào)試服務(wù)

1、要調(diào)試服務(wù),其實(shí)很簡(jiǎn)單,如需將服務(wù)附加進(jìn)程到需要調(diào)試的項(xiàng)目里面即可,假如要調(diào)試剛才建的服務(wù),現(xiàn)在OnStop事件里設(shè)置斷點(diǎn),如下所示:

 

2、啟動(dòng)“WindowsServiceClient”項(xiàng)目,在“調(diào)試”菜單中選擇“附件到進(jìn)程”(服務(wù)必須事先安裝),如下所示:


3、找到“MyWindowsService.exe”,點(diǎn)擊“附加”按鈕,如下圖所示:


4、點(diǎn)擊“停止服務(wù)”按鈕,程序?qū)?huì)在設(shè)置斷點(diǎn)的地方中斷,如下圖所示:



該文章在 2021/1/29 16:59:27 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 东京热无码一区 | 国产麻豆精品 | 国产探花 | 91黑丝半腿过膝吊带丝袜 | 高潮流白浆喷水正在播放 | 观看亚洲中文无码 | 国产精品成人不卡在线观看 | 国产精品成人亚洲毛片 | 国产精品国产自线拍免费丝 | 国产99久久99热这 | 国产精品成人免费观看 | 国产一级毛片农村寡妇 | 福利视频一区二区三区四区 | av在线播放免费无码 | 国产一区二区三区不卡 | 丰满爆乳无码专区一区 | www.一区二区三区 | 国产日韩无码中文字幕在线综合 | 精品人妻无码专区在中文字 | 变态另类国产精品制服丝袜 | 国产成年女人免费视频播放a | 91在线国产专区精品 | 国产无码影视 | 国产黑色丝袜在线观看一区 | 高潮国产精品一区二区 | 国产日韩欧美馆免费观看 | 国产欧美视频一区二区不卡 | 国产午夜福利精品一区二区三区 | 国产成人αv无码专区亚洲αv | 国产成人无码 | 国产va在线 | 18禁无码动漫在线播放 | 精品人妻一区二区三区四区 | 国产精品视频无圣光一区 | 国产欧美日韩亚洲另类 | 18精品爽国产白嫩精品 | 国产女主播一二三区丝袜美腿 | 精品婷婷色一区二区三区 | 国产高清在线播放刘婷91 | 精品亚洲午夜久 | 成人欧美网日韩青椒网 |