Link to home
Create AccountLog in
Avatar of ferocious
ferocious

asked on

Can't Stop/Start Windows service from C# Progammatically

Hey everyone,
I am trying to communicate with a Windows Service.but i am getting some error.please help me.
Right now i'm running as a Administrator and i have a Windows form application where i have a start and a stop button
and this is what it does

stop button

            serviceController1.MachineName = "myMachine";
             serviceController1.ServiceName = "iPod Service";
            if (serviceController1.Status.Equals(ServiceControllerStatus.Stopped))
            {
                serviceController1.Start();
            }

Start Button

           serviceController1.MachineName = "myMachine";
            serviceController1.ServiceName = "iPod Service";
            if (serviceController1.Status.Equals(ServiceControllerStatus.Running))
            {
                serviceController1.Stop();
            }


And this is giving me some error messages as follows :

Cannot open iPod Service service on computer 'myMachine"


System.InvalidOperationException was unhandled
  Message=Cannot open iPod Service service on computer 'myMachine'.
  Source=System.ServiceProcess
  StackTrace:
       at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
       at System.ServiceProcess.ServiceController.Start(String[] args)
       at System.ServiceProcess.ServiceController.Start()
       at QData.ResetAccount.btnStop_Click(Object sender, EventArgs e) in F:\Users\MyACCOUNT\Desktop\ccount.cs:line 114
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at QData.Program.Main() in F:\Users\****\Desktop\:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ComponentModel.Win32Exception
       Message=Access is denied
       ErrorCode=-2147467259
       NativeErrorCode=5
       InnerException:

Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

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
  {
    // ...
  }
}



public static void StopService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  }
  catch
  {
    // ...
  }
}
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of ferocious
ferocious

ASKER

hey thanks for your help but i still get the same error message as before
which is
 " Cannot open 'Service Name' service on Computer 'myMachine'.  "
and it does the same for the stop function,i don't  know why it's doing this to me.
because i have followed similar tutorials/examples from a number of websites and still getting the same error.

I have also tried using command Prompt :
sc start gusvc(Google Updater Service) and it gives me error message " Access is Denied".

but if i run the Command Prompt by right clicking run as Administrator then i can start or stop a service.
so i believe something is wrong with the access rights.
please some one help me.
thanks
u have to place your service name in place of ServiceName

ServiceController service = new ServiceController(<YOUR SERVICE NAME);
I did put the service name which i want to start or stop and I wrote "Service Name" here for explanation purpose.
thanks
run the service as local system account
I ran the service as a local system and i am still getting the same error. i have no idea what have i done wrong.
thanks