2012-06-16 9 views

उत्तर

54

का उपयोग कर System.ServiceProcess.dll के लिए एक संदर्भ जोड़ें रोक सकता है। फिर आप ServiceController कक्षा का उपयोग कर सकते हैं।

// Check whether the Alerter service is started. 
ServiceController sc = new ServiceController(); 
sc.ServiceName = "Alerter"; 
Console.WriteLine("The Alerter service status is currently set to {0}", 
        sc.Status.ToString()); 

if (sc.Status == ServiceControllerStatus.Stopped) 
{ 
    // Start the service if the current status is stopped. 
    Console.WriteLine("Starting the Alerter service..."); 
    try 
    { 
     // Start the service, and wait until its status is "Running". 
     sc.Start(); 
     sc.WaitForStatus(ServiceControllerStatus.Running); 

     // Display the current service status. 
     Console.WriteLine("The Alerter service status is now set to {0}.", 
         sc.Status.ToString()); 
    } 
    catch (InvalidOperationException) 
    { 
     Console.WriteLine("Could not start the Alerter service."); 
    } 
} 
+0

यह सिस्टम.ServiceProcess का उपयोग करके इसे पहचान नहीं करता है; - मैं .NET 4 – AlexandruC

+0

@lxClan का उपयोग कर रहा हूं अपनी परियोजना – Zbigniew

+0

में संदर्भ जोड़ें मैंने उस संदर्भ के साथ उत्तर को अपडेट किया है जिसे आपको जोड़ने की आवश्यकता है। –

2

आप इसे इस तरह से कर सकते हैं, Details of Service Controller

ServiceController sc = new ServiceController("your service name"); 
if (sc.Status == ServiceControllerStatus.Stopped) 
{ 
    sc.Start(); 

} 

इसी तरह आप बंद विधि

sc.Stop(); 
+0

यह इस System.ServiceProcess का उपयोग कर को नहीं पहचानता है पर अमल; - मैं .NET 4 – AlexandruC

+1

का उपयोग कर रहा हूं नामस्थान जोड़ें, शायद आप इसे याद कर रहे हैं। – edocetirwi

15

पहले सिस्टम.ServiceProcess असेंबली का संदर्भ जोड़ें।

शुरू करने के लिए:

ServiceController service = new ServiceController("YourServiceName"); 
service.Start(); 
var timeout = new TimeSpan(0, 0, 5); // 5seconds 
service.WaitForStatus(ServiceControllerStatus.Running, timeout); 

रोकने के लिए:

ServiceController service = new ServiceController("YourServiceName"); 
service.Stop(); 
var timeout = new TimeSpan(0, 0, 5); // 5seconds 
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); 

दोनों उदाहरण जब तक सेवा एक नई स्थिति तक पहुँच गया है प्रतीक्षा करने के लिए कैसे पता चलता है (चल रहा है, बंद कर दिया ... आदि।)। WaitForStatus में टाइमआउट पैरामीटर वैकल्पिक है।

+0

यह सिस्टम.ServiceProcess का उपयोग करके इसे पहचान नहीं करता है; - मैं .NET 4 – AlexandruC

+0

का उपयोग कर रहा हूं ठीक काम करना चाहिए, लेकिन आपको System.ServiceProcess का संदर्भ जोड़ना होगा। –

+0

सही! मुझे मूर्ख। धन्यवाद। चिह्नित! – AlexandruC

0

एक गंदी, लेकिन एक ही एक ही है ..
सिर्फ शेल कमांड

NET STOP "MYSERVICENAME" 
NET START "MYSERVICENAME" 
0
// Check whether the U-TEST RTC service is started. 
     ServiceController sc = new ServiceController(); 
     sc.ServiceName = "U-TEST RTC"; 
     m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStatusService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 

     if (sc.Status == ServiceControllerStatus.Stopped) 
     { 
      m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      try 
      { 
       // Start the service, and wait until its status is "Running". 
       sc.Start(); 
       var timeout = new TimeSpan(0, 0, 5); // 5seconds 
       sc.WaitForStatus(ServiceControllerStatus.Running, timeout); 
       m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgNowService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      } 
      catch (InvalidOperationException) 
      { 
       m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgExceptionStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      } 
     } 
संबंधित मुद्दे