Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows Service Controlle in VS2008

Windows Service Controlle in VS2008

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestionlearning
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Whitaker Mike
    wrote on last edited by
    #1

    Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike

    L D S 3 Replies Last reply
    0
    • W Whitaker Mike

      Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you are not getting an exception how do you know the service has not started? Depending on what OS you are on, the OS properties are reported diffrently. Take a look at the win32 to .NET API cross refrence. Microsoft Win32 to Microsoft .NET Framework API Map[^]

      1 Reply Last reply
      0
      • W Whitaker Mike

        Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        I could be way off here but if you're using Vista (or possibly Weven), IIRC you can only stop and start services if you add a manifest file and elevate to administrator (yeah - that brings in the UAC prompt for your app). It's a long time since I've tried this so I may be wrong, but I think that this is the case, and also if my memory serves correctly, you don't get any exception - it just doesn't work.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • W Whitaker Mike

          Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike

          S Offline
          S Offline
          Shukla Rahul
          wrote on last edited by
          #4

          Hi Mike, It did work for me on VS 2008. Here is my program - using System; using System.ServiceProcess; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { ServiceController service = new ServiceController("<<Your Service Name>>"); service.Stop(); TimeSpan timeout = TimeSpan.FromMilliseconds(10000); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); if (service.Status == ServiceControllerStatus.Stopped) { service.Start(); } } catch { // ... } } } } Are you getting any specific error ?

          W 1 Reply Last reply
          0
          • S Shukla Rahul

            Hi Mike, It did work for me on VS 2008. Here is my program - using System; using System.ServiceProcess; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { ServiceController service = new ServiceController("<<Your Service Name>>"); service.Stop(); TimeSpan timeout = TimeSpan.FromMilliseconds(10000); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); if (service.Status == ServiceControllerStatus.Stopped) { service.Start(); } } catch { // ... } } } } Are you getting any specific error ?

            W Offline
            W Offline
            Whitaker Mike
            wrote on last edited by
            #5

            First of all I would like to thank every one who has tried to help me thus far, it great! As I said im a noob at c# getting this type of help will bring me to the next level (even if its a still a low one ;)) I'm am using vista.... when using Shukla's great piece of code I get this: The problem might be missing a reference? Error 1 The type or namespace name 'ServiceProcess' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\mike\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 2 14 ConsoleApplication1 The problem might be missing a reference? Also the ebook (Microsoft.Press.Microsoft.Visual.C.Sharp.2008.Step.by.Step)I use to teach myself is not telling me how to link in a external .dll so if you have any good references on this subject I would love to know! Anyway thank you for you time! :-D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups