Quantcast
Channel: Ömer Çelik - c#
Viewing all articles
Browse latest Browse all 3

Windows Servislerini stop - start etmek

$
0
0

Zaman zaman sunucu üzerindeki servislerin durumunu öğrenmek, stop-start veya restart etme gibi ihtiyaçlar doğmaktadır.

Windows Servislerini aşağıdaki methodları kullanarak stop - start edebilirsiniz. 


        publicbool Stop-Service(string servicename, string MachineName)

        {

            try

            {

                TimeSpan timeout = TimeSpan.FromSeconds(30);

                ServiceController service = newServiceController(servicename, MachineName);

                if (service.Status == ServiceControllerStatus.Running)

                {

                    Console.Write(service.MachineName + " " + service.ServiceName + " Durduruluyor");

                    service.Stop();

                    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                    Console.WriteLine(" - Durduruldu");

                }

                service.Dispose();

 

                returntrue;

            }

            catch (Exception ex)

            {

                returnfalse;

            }

        }

        publicbool StartService(string servicename, string MachineName)

        {

            try

            {

                TimeSpan timeout = TimeSpan.FromSeconds(30);

                ServiceController service = newServiceController(servicename, MachineName);

                if (service.Status == ServiceControllerStatus.Stopped)

                {

                    Console.Write(service.MachineName + " " + service.ServiceName + " Başlatılıyor");

                    service.Start();

                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);

                    Console.WriteLine(" - Başlatıldı");

                }

                service.Dispose();

 

                returntrue;

            }

            catch (Exception ex)

            {

                returnfalse;

            }

        }


Viewing all articles
Browse latest Browse all 3

Latest Images