vs2019 創建Windows Service

  •  首先創建一個Windows服務程序

  •  編譯器生成項目如下:
  •  雙擊右側Service1.cs,然後在左側右鍵單機添加安裝程序

 

 如圖,我們看見了2個控件

右鍵serviceInstaller1單擊屬性

 將ServiceName修改爲TestService,這將是我們服務中顯示的名稱。

然後右鍵serviceProcessInstaller1單擊屬性,設置Account這代表着用那種用戶啓動這個服務

  • 代碼編寫

Program.cs中Main方法是整個程序的起點,並且可以調用多個服務程序

 在服務代碼中,OnStart和OnStop只會執行一次,分別在開始和停止的時候。除此之外還有OnContinue,OnPause等等,詳細請在ServiceBase處按F12,從元數據。

namespace TestWindowsService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                this.WriteLog("服務啓動");
                this.ToDoSomeThings();
            }
            catch (Exception ex)
            {
                this.WriteLog(ex.Message);
                ServiceController serviceController = new ServiceController();
                if (serviceController.CanStop)
                {
                    serviceController.Stop();
                }
            }
            
        }

        protected override void OnStop()
        {
            try
            {
                this.WriteLog("服務結束");
            }
            catch (Exception ex)
            {
                this.WriteLog(ex.Message);
                ServiceController serviceController = new ServiceController();
                if (serviceController.CanStop)
                {
                    serviceController.Stop();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void ToDoSomeThings() 
        {
            DateTime dateTime = DateTime.Now;
            if (dateTime.Hour == 14 && dateTime.Minute == 30)
            {
                int a = 0;
                for (int i = 0; i < 10000; i++)
                {
                    a += i;
                }
                this.WriteLog(a.ToString());
            }
            
        }
        /// <summary>
        /// 記錄日誌
        /// </summary>
        /// <param name="LogContent"></param>
        private void WriteLog(string LogContent) 
        {
            string FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\Log.txt";
            FileStream fileStream = null;
            if (File.Exists(FilePath))
            {
                fileStream = new FileStream(FilePath, FileMode.Append);
            }
            else
            {
                fileStream = new FileStream(FilePath, FileMode.OpenOrCreate);
            }
            StreamWriter streamWriter = new StreamWriter(fileStream);
            streamWriter.WriteLine(DateTime.Now + System.Environment.NewLine + LogContent);
            
            streamWriter.Close();
            fileStream.Close();
            GC.Collect();
        }
    }
}
  •  保存並生成後,將服務安裝

 詳細安裝方式見上一篇

https://blog.csdn.net/tx1721110240/article/details/102649542

  • 調試方式

注:因爲服務未啓動時無法產生進程,所以一般調試都會在項目中再創建一個控制檯或窗體,調用需要調試的方法達到調試的目的,而不會使用這種調試方式。

首先啓動服務

在vs中調試-》附加到進程

 選擇和項目名稱一樣的進程名,點擊附加後就可以正常調試了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章