zoukankan      html  css  js  c++  java
  • ServicesController 的使用

    using System;
    using System.ServiceProcess;
    using System.Diagnostics;
    using System.Threading;
    
    namespace ServiceControllerSample
    {
        class Program
        {
            public enum SimpleServiceCustomCommands
            { StopWorker = 128, RestartWorker, CheckWorker };
            static void Main(string[] args)
            {
                ServiceController[] scServices;
                scServices = ServiceController.GetServices();
    
                foreach (ServiceController scTemp in scServices)
                {
    
                    if (scTemp.ServiceName == "Simple Service")
                    {
                        // Display properties for the Simple Service sample
                        // from the ServiceBase example.
                        ServiceController sc = new ServiceController("Simple Service");
                        Console.WriteLine("Status = " + sc.Status);
                        Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
                        Console.WriteLine("Can ShutDown = " + sc.CanShutdown);
                        Console.WriteLine("Can Stop = " + sc.CanStop);
                        if (sc.Status == ServiceControllerStatus.Stopped)
                        {
                            sc.Start();
                            while (sc.Status == ServiceControllerStatus.Stopped)
                            {
                                Thread.Sleep(1000);
                                sc.Refresh();
                            }
                        }
                        // Issue custom commands to the service
                        // enum SimpleServiceCustomCommands 
                        //    { StopWorker = 128, RestartWorker, CheckWorker };
                        sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
                        sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
                        sc.Pause();
                        while (sc.Status != ServiceControllerStatus.Paused)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                        Console.WriteLine("Status = " + sc.Status);
                        sc.Continue();
                        while (sc.Status == ServiceControllerStatus.Paused)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                        Console.WriteLine("Status = " + sc.Status);
                        sc.Stop();
                        while (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                        Console.WriteLine("Status = " + sc.Status);
                        String[] argArray = new string[] { "ServiceController arg1", "ServiceController arg2" };
                        sc.Start(argArray);
                        while (sc.Status == ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                        Console.WriteLine("Status = " + sc.Status);
                        // Display the event log entries for the custom commands
                        // and the start arguments.
                        EventLog el = new EventLog("Application");
                        EventLogEntryCollection elec = el.Entries;
                        foreach (EventLogEntry ele in elec)
                        {
                            if (ele.Source.IndexOf("SimpleService.OnCustomCommand") >= 0 |
                                ele.Source.IndexOf("SimpleService.Arguments") >= 0)
                                Console.WriteLine(ele.Message);
                        }
                    }
                }
    
    
            }
        }
    }
    // This sample displays the following output if the Simple Service
    // sample is running:
    //Status = Running
    //Can Pause and Continue = True
    //Can ShutDown = True
    //Can Stop = True
    //Status = Paused
    //Status = Running
    //Status = Stopped
    //Status = Running
    //4:14:49 PM - Custom command received: 128
    //4:14:49 PM - Custom command received: 129
    //ServiceController arg1
    //ServiceController arg2
    

    以上代码转自msdn:http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicecontroller(v=vs.110).aspx

    今天自己调试了一个ServicesController ,本来一个停止服务由于没有刷新,始终在一个状态下

     while (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            
                        }
    

      如果是怎么写的话,状态会一直在Stoppending

    需要加入一句sc.Refresh();

     while (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
    
    
  • 相关阅读:
    zoj 3627#模拟#枚举
    Codeforces 432D Prefixes and Suffixes kmp
    hdu 4778 Gems Fight! 状压dp
    CodeForces 379D 暴力 枚举
    HDU 4022 stl multiset
    手动转一下田神的2048
    【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律
    poj 3254 状压dp
    C++中运算符的优先级
    内存中的数据对齐
  • 原文地址:https://www.cnblogs.com/crazycxy/p/3753254.html
Copyright © 2011-2022 走看看