C#使用定時任務架構Windows.TaskSchedule.exe安裝控制台應用程式建立的Windows服務

來源:互聯網
上載者:User

標籤:拷貝   windows服務   stream   tin   ext   isp   www   工作管理員   文本   

上一篇介紹了利用模板建立、安裝以及卸載Windows服務,本篇繼續研究這個話題,不過是建立控制台程式,使用Windows.TaskSchedule.exe安裝

為什麼要使用控制台程式?——調試簡單粗暴。哈哈。

建立控制台應用程式項目後,我們編輯服務主體代碼,我們繼續向文字檔中寫入一句話。

在實現具體邏輯前,我們需要引用Windows.TaskSchedule.Extends.dll,服務類繼承 IJob

 1 using System; 2 using System.Configuration; 3 using System.IO; 4 using Windows.TaskSchedule.Extends; 5  6 namespace MyWindowsTaskSchedule 7 { 8     /// <summary> 9     /// 具體服務邏輯實現10     /// </summary>11     public class Test : IJob12     {13         public void Excute()14         {15             //throw new NotImplementedException();16             FileStream fs = new FileStream(ConfigurationManager.AppSettings["LogFile"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);17             StreamWriter sw = new StreamWriter(fs);18             sw.BaseStream.Seek(0, SeekOrigin.End);19             sw.WriteLine(string.Format("\"MyWindowsTaskSchedule\" Windows Service Run At {0} \n", DateTime.Now.ToString()));20             sw.Flush();21             sw.Close();22             fs.Close();23         }24 25         public void Init()26         {27             //throw new NotImplementedException();28         }29 30         public void OnError(Exception ex)31         {32             //throw new NotImplementedException();33         }34     }35 }

主程式入口調用服務

 1 namespace MyWindowsTaskSchedule 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             var test = new Test(); 8             test.Excute();  //運行服務 9         }10     }11 }

編譯器,bin目錄的檔案就是服務即將要跑的。

 

那麼問題來了,我怎麼跑?

 

我先附上一個再做簡單說明。

Bin目錄:存放控制台編譯後的檔案,包括設定檔。

Configs目錄:兩個設定檔Jobs.config和NLog.config

Jobs.config配置如下:

1 <?xml version="1.0" encoding="utf-8" ?>2 <Jobs serverName="my-service" displayName="我的服務" description="我的服務描述">3   <Job name="我的服務一" exePath="${basedir}\Bin\MyWindowsTaskSchedule.exe" arguments="1" cornExpress="0/2 * * * * ?" expireSecond="300" />4 </Jobs>

serverName:服務在工作管理員中顯示的名稱

displayName:服務在服務列表中顯示的名稱

Job節點:各個任務的具體配置,name建議唯一,exePath任務路徑,cornExpress任務執行頻率

實際應用中,我們需要修改Jobs.config,NLog.config可以不動。

 

安裝服務.bat

1 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule.exe install2 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule.exe start3 pause

卸載服務.bat

1 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule.exe stop2 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule.exe uninstall3 pause

以上兩個批處理命令,Windows.TaskSchedule.exe前面一大串,請根據需要調整。

其他檔案直接拷貝過來即可。

 

萬事俱備後(上面那個Demo目錄),我們以管理員身份運行“安裝服務.bat”,如下:

然後,我們可以在服務列表及工作管理員中檢查

 

如果運行“卸載服務.bat”,效果如下:

源碼及Demo下載:https://github.com/maiaimei/WindowsTaskSchedule

參考網址:

開源一個windows下的定時任務架構,簡單粗暴好用。

C#使用定時任務架構Windows.TaskSchedule.exe安裝控制台應用程式建立的Windows服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.