C 建立並使用服務

2021-10-14 08:23:28 字數 1345 閱讀 3422

建立乙個控制台程式

新建乙個windows服務項

右鍵新增安裝程式

設定以下配置項

// 

// serviceinstaller1

// this.serviceinstaller1.description = "學習使用服務"; //服務中的描述

this.serviceinstaller1.displayname = "testservice"; //服務名稱

this.serviceinstaller1.servicename = "testservice";

this.serviceinstaller1.starttype = system.serviceprocess.servicestartmode.automatic;

//選擇serviceprocessinstaller1項設定 服務為本地服務

this.serviceprocessinstaller1.account = system.serviceprocess.serviceaccount.localsystem;

開啟自己建立的服務類檔案

在onstart方法中寫入以下**

#if debug

if (!debugger.isattached)

debugger.launch();

debugger.break();

#endif

//啟動服務要做什麼事情全在這呼叫

startweatherservice();

//進行相應的操作

private void startweatherservice()

);logwriter.writer(datetime.now + ":服務已經啟動!");

}catch (exception ex)

}建立 testservice 類

public class testservice

private void _timer_elapsed(object sender, system.timers.elapsedeventargs e)

catch (exception ex)}}

最後,在控制台引用程式中加入以下**

class program

;servicebase.run(servicestorun);}}

安裝服務

cd c:\windows\microsoft.net\framework\v4.0.30319

解除安裝服務

效果

使用C 建立windows服務

前言 說幹就幹!開發環境 作業系統 windows 10 x64 開發環境 vs2017 程式語言 c net版本 net framework 4.5 目標平台 x86 windows service簡介 乙個windows服務程式是在windows作業系統下能完成特定功能的可執行的應用程式。win...

C 建立系統服務並定時執行

c 建立系統服務並定時執行 1.新建專案 windows 服務 2.service1.cs view plaincopy to clipboardprint?using system using system.collections.generic using system.componentmod...

建立並啟動MongoDB服務

按照如下命令來建立並啟動mongodb服務,就可以通過windows服務來管理mongodb的啟動和關閉了 mongod config d mongo mongo.conf install servicename mongodb net start mongodb 上面是2行的命令 指示 mongo...