2

Можно ли остановить службы циклом? У меня есть список служб, занёс их в List

 List<string> name = new List<string> { "AdobeARMservice", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV", "Telnet",
             "mnmsrvc", "Schedule", "seclogon", "SessionEnv", "bthserv",
             "WinRM", "SCardSvr", "WbioSrvc", "SCPolicySvc","RtkBleServ"};

Как-то так попытался:

 foreach (ServiceController service in ServiceController.GetServices())
            {
                if (service.ServiceName == name[15])
                {
                    service.Stop();
                    Console.WriteLine("Остановлен");
                }
            }

Или как можно по другому?

Решил вопрос:

на Linq!

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

namespace ConsoleApplication25 {
    class Program {
        static List<string> services = new List<string> {
            "AdobeARMService", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV"
        };
        static void Main(string[] args) {
            Task.Factory.StartNew(() => Run());
            Console.ReadLine();
        }
        static void Run() {
            while (true) {
                var temp = ServiceController.GetServices().Where(s => services.Contains(s.ServiceName)).ToList();
                temp.ForEach(t => {
                    try {
                        t.Stop();
                    }
                    catch { }
                });
                Thread.Sleep(500);
            }
        }
    }
}

без Linq

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

namespace ConsoleApplication25 {
    class Program {
        static List<string> services = new List<string> {
            "AdobeARMService", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV"
        };
        static void Main(string[] args) {
            new Thread(Run) { IsBackground = true }.Start();
            Console.ReadLine();
        }
        static void Run() {
            while (true) {
                List<ServiceController> temp = new List<ServiceController>();
                foreach (var s in ServiceController.GetServices()) {
                    if (services.Contains(s.ServiceName)) {
                        temp.Add(s);
                    }
                }
                foreach (var s in temp) {
                    try {
                        s.Stop();
                    }
                    catch { }
                }
                Thread.Sleep(500);
            }
        }
    }
}

1 ответ 1

0

Можно сделать так, что бы конструкторы служб принимали в качестве аргумента CancellationToken и в самих службах вызывать метод ThrowIfCancellationRequested(), время от времени.

Тогда можно будет вызвать из основного кода CancellationTokenSource.Cancel и службы массово завершаться.

1
  • Можете пример привести пожалуйста
    – GooliveR
    1 окт 2016 в 21:48

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.