第一个WindowService服务
生活随笔
收集整理的這篇文章主要介紹了
第一个WindowService服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景:Web項目中需要定時執行一段程序
方法: 1.新建一個WindowService項目
2.添加代碼
public partial class Service1 : ServiceBase{System.Timers.Timer timer = null;public Service1(){InitializeComponent();}protected override void OnStart(string[] args){timer = new System.Timers.Timer();timer.Elapsed += timer_Elapsed;timer.Interval = 10000;timer.Start();}void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e){if (!Debugger.IsAttached){Debugger.Launch();}Console.WriteLine("Time elapsed");}protected override void OnStop(){if (timer != null){timer.Stop();}}}3.添加安裝程序
3.1修改serviceProcessInstaller1屬性中的account
3.2 修改serviceInstaller1屬性中的ServiceName(此名稱為服務的名稱)
4.添加外部工具
?
5.運行
可以看到成功了
6.刪除服務 cmd.exe ?
sc delete "ServiceName"
轉載于:https://www.cnblogs.com/engineerlm/p/7260471.html
總結
以上是生活随笔為你收集整理的第一个WindowService服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CLR类型设计之属性
- 下一篇: Mozilla “Common Voic