2013年5月8日 星期三

[C#] Windows Service 的啟動、停止和重新啟動的功能

       Windows Service除了手動到服務去做啟動、停止和重新啟動之外,也可以透過程式去做這些動作。程式碼如下:


        /// <summary>
        /// 啟動服務。
        /// </summary>
        /// <param name="serviceName">服務名稱。</param>
        /// <param name="timeoutMilliseconds">其指定等候服務到達指定之狀態的時間長度。</param>
        /// <returns>執行結果成功或失敗。</returns>
        public static bool StartService(string serviceName, int timeoutMilliseconds)
        {
            bool processResult = false;
            ServiceController service = new ServiceController(serviceName);
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                processResult = true;
            }
            catch (Exception e)
            {
                processResult = false;
            }
            return processResult;
        }

        /// <summary>
        /// 停止服務。
        /// </summary>
        /// <param name="serviceName">服務名稱。</param>
        /// <param name="timeoutMilliseconds">其指定等候服務到達指定之狀態的時間長度。</param>
        /// <returns>執行結果成功或失敗。</returns>
        public static bool StopService(string serviceName, int timeoutMilliseconds)
        {
            bool processResult = false;
            ServiceController service = new ServiceController(serviceName);
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                processResult = true;
            }
            catch (Exception e)
            {
                processResult = false;
            }
            return processResult;
        }

        /// <summary>
        /// 重新啟動服務。
        /// </summary>
        /// <param name="serviceName">服務名稱。</param>
        /// <param name="timeoutMilliseconds">其指定等候服務到達指定之狀態的時間長度。</param>
        /// <returns>執行結果成功或失敗。</returns>
        public static bool RestartService(string serviceName, int timeoutMilliseconds)
        {
            bool processResult = false;
            ServiceController service = new ServiceController(serviceName);
            try
            {
                int millisec1 = Environment.TickCount;
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                // count the rest of the timeout
                int millisec2 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                processResult = true;
            }
            catch (Exception e)
            {
                processResult = false;
            }
            return processResult;
        }