建立乙個簡單WindowsService

2022-07-15 01:18:11 字數 2770 閱讀 1912

建立乙個簡單windowsservice

1.建立乙個專案:

選擇windows 服務 名稱為servicetest

2.重新命名為servicetest

彈出下列彈框時選擇是

3.新增安裝程式:

雙擊

在設計視窗對應標籤下右擊 選擇新增安裝程式

右擊 serviceinstaller1 選擇屬性

servicename修改為servicetest

右擊 serviceprocessinstaller1 選擇屬性

account修改為localservice

4.編寫**右擊 servicetest.cs檔案

選擇檢視**

新增**如下:

using

system.serviceprocess;

namespace

servicetest

//////

服務啟動時

/// ///

protected

override

void onstart(string

args)

servicetest已經啟動

", system.datetime.now.tostring("

yyyy-mm-dd:hh:mm:ss

"));}}

//////

服務停止時

/// protected

override

void

onstop()

servicetest已經停止

", system.datetime.now.tostring("

yyyy-mm-dd:hh:mm:ss

"));}}

}}

新增安裝批處理檔案 install.bat 到專案中 內容如下(使用記事本的話,請儲存完ansi格式,否則部分轉義符號可能不被識別)

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe %~dp0\bin\debug\servicetest.exe

net start servicetest

sc config servicetest start=auto

pause

這個批處理的意思是 呼叫%systemroot%\microsoft.net\framework\v4.0.30319\  批處理安裝程式 installutil.exe 安裝批處理檔案所在目錄下的bin\debug\servicetest.exe 程式

啟動服務servicetest

並設定服務servicetest 為自動

pause 是讓批處理執行完之後暫停已檢視結果

新增解除安裝處理程式 uninstall.bat 到專案中 內容如下(使用記事本的話,請儲存完ansi格式,否則部分轉義符號可能不被識別)

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u %~dp0\bin\debug\servicetest.exe

pause

這個批處理的意思是 呼叫%systemroot%\microsoft.net\framework\v4.0.30319\  批處理安裝程式 installutil.exe 解除安裝批處理檔案所在目錄下的bin\debug\servicetest.exe 程式

pause 是讓批處理執行完之後暫停已檢視結果

整個目錄如下:

生成一下整個專案 先執行install.bat

服務已經啟動成功了,檢視日誌

如果報錯的話,請以管理員許可權執行指令碼

如果出現 服務無法啟動 服務沒有報告任何錯誤

多半是程式有問題,可以檢查windows事件檢視器

建立乙個簡單的執行緒

handle createthread in opt lpsecurity attributes lpthreadattributes,security attributes 結構指定了這個執行緒的安全屬性,如果填 null 則就以預設的安全描述子建立,並且返回的控制代碼不會被繼承。in size ...

建立乙個簡單的畫板

1.cv.createtrackbar 有5個引數 滑動條的名字 視窗的名字 數值的最小值 數值的最大值 函式2.cv.gettrackbarpos 有2個引數 滑動條的名字 視窗的名字 函式的返回值是滑動條的數值。import cv2 as cv import numpy as np def no...

建立乙個簡單的systemverilog程式

作為乙個物件導向的語言,免不了來乙個國際慣例 hello world!下面是完整的 program tb initial begin display hello,world endendprogram 用modelsim進行編譯和執行 hello,world 解釋 跟c語言的main函式類似,sv語...