程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 開發Windows Service程序控制功能

C# 開發Windows Service程序控制功能

編輯:C#入門知識

C# 開發Windows Service程序控制功能


在做一些計劃任務時候難免用到Windows Service服務程序,而這個是沒有操作界面的,每次啟動、重啟、關閉都需要服務界面找到服務進行操作,對普通的人來說是非常麻煩的,所以有時候就需要通過應用程序來控制Windows 服務,這裡把之前寫到的一個服務控制類貼出來。

服務設置截圖

C# Windows 服務控制類代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;

namespace Tools.App
{
    public class ServiceHelper
    {
        ///   
        /// Restart windows service  
        ///   
        ///the windows service display name  
        ///  If the restart successfully return true else return false  
        public static bool RestartWindowsService(string serviceName)
        {
            bool bResult = false;
            try
            {
                try
                {
                    StopWindowsService(serviceName);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    StartWindowsService(serviceName);
                    Thread.Sleep(1000);
                    StopWindowsService(serviceName);
                    Thread.Sleep(1000);
                    Console.WriteLine(ex.Message);
                }
                try
                {
                    StartWindowsService(serviceName);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    StopWindowsService(serviceName);
                    Thread.Sleep(1000);
                    StartWindowsService(serviceName);
                    Thread.Sleep(1000);
                    Console.WriteLine(ex.Message);
                }
                bResult = true;
            }
            catch (Exception ex)
            {
                bResult = false;
                throw ex;
            }
            return bResult;
        }

        ///   
        /// Start windows service  
        ///   
        ///the windows service display name  
        /// If the start successfully return true else return false  
        public static bool StopWindowsService(string serviceName)
        {
            ServiceController[] scs = ServiceController.GetServices();
            bool bResult = false;
            foreach (ServiceController sc in scs)
            {
                if (sc.ServiceName == serviceName)
                {
                    try
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(1000 * 30));
                        sc.Stop();
                        bResult = true;
                    }
                    catch (Exception ex)
                    {
                        bResult = false;
                        throw ex;
                    }
                }
            }
            return bResult;
        }

        ///   
        /// Stop windows service  
        ///   
        ///the windows service display name  
        /// If the stop successfully return true else return false  
        public static bool StartWindowsService(string serviceName)
        {
            ServiceController[] scs = ServiceController.GetServices();
            bool bResult = false;
            foreach (ServiceController sc in scs)
            {
                if (sc.ServiceName == serviceName)
                {
                    try
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(1000 * 30));
                        sc.Start();
                        bResult = true;
                    }
                    catch (Exception ex)
                    {
                        bResult = false;
                        throw ex;
                    }
                }
            }
            return bResult;
        }


        public static bool ServiceIsExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName == serviceName)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

窗體按鈕事件調用代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;

namespace Tools.App
{
    public partial class ServiceForm : Form
    {
        public ServiceForm()
        {
            InitializeComponent();
        }


        /// 
        /// 加載判斷服務是否存在
        /// 
        ///
        ///
        private void ServiceForm_Load(object sender, EventArgs e)
        {

            if (ServiceHelper.ServiceIsExisted(TaskTimer))
            {
                ServiceController service = new ServiceController(TaskTimer);
                if (service.Status != ServiceControllerStatus.Stopped && service.Status != ServiceControllerStatus.StopPending)
                {
                    this.btnStart.Enabled = false;
                    this.button2.Enabled = true;
                }
                else
                {
                    this.btnStart.Enabled = true;
                    this.button2.Enabled = false;
                }

            }
        }

        /// 
        /// 啟動
        /// 
        ///
        ///
        private void btnStart_Click(object sender, EventArgs e)
        {
            ServiceHelper.StartWindowsService(TaskTimer);
            this.btnStart.Enabled = false;
            this.button2.Enabled = true;
            MessageBox.Show(啟動成功, 提示);
        }

        /// 
        /// 關閉
        /// 
        ///
        ///
        private void btnClose_Click(object sender, EventArgs e)
        {
            ServiceHelper.StopWindowsService(TaskTimer);
            this.btnStart.Enabled = true;
            this.button2.Enabled = false;
            MessageBox.Show(關閉成功, 提示);
        }

        private void btnIsExisted_Click(object sender, EventArgs e)
        {
            bool bl = ServiceHelper.ServiceIsExisted(TaskTimer);
            if (bl)
            {
                MessageBox.Show(TaskTimer 存在!, 提示);
            }
            else
            {
                MessageBox.Show(TaskTimer 不存在!, 提示);
            }
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved