By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Unable to stop the Windows service.

P: 4
I am creating an object of servicecontroller() as follows

system,.serviceprocess.servicecontroller objService = new servicecontroller(Service Name);

Iam trying to stop the service using

objService.Stop(); but i am unable to start the service.

I tried by writing the above statement 2 times,

objService.Stop();

objService.Stop();

now iam able to stop the service. why so?

please help me.
Jan 20 '09 #1
Share this Question
Share on Google+
6 Replies


P: 4
In detail for your better understanding:

There is an overriden method to start the service and stop the service.

I am creating an object of servicecontroller() like

system.serviceprocess.servicecontroller objService = new servicecontroller(Service Name);

protected override void OnStart(String[] args)
{
--------------
-----------------//writing some code
if()
{
//Performs some task
}
else
{
objService.Stop(); //
---------some message "Failed to start the service" and
the service should not start. But the Service gets Started.This is the
Problem iam facing.
}
}

If my code tries to execute else part
it should execute

objService.Stop(); but i am unable to stop the service.

I tried by writing the above statement 2 times,

objService.Stop();
objService.Stop();

now iam able to stop the service. why so?
please help me.Its very urgent


If U can help me ,i'll be so glad and Please Reply me as soon as possible.
Jan 20 '09 #2

Plater
Expert 5K+
P: 7,872
I don't think you are the supposed to call the .start() and .stop() methods from withen your own service?
When the end of your onStart() method is reached, if it did not start any threads that have not yet stopped, it will exit and the service will stop automatically.
Jan 20 '09 #3

P: 4
Thanx for the reply.

I need a case, we want to stop the service through the progam.
But in the program, if we want to stop the service

we can stop the service by .Stop() method . My program does not execute this if i write .Stop() once.

But if i write 2 times .Stop() method .then it will not allow the service to start.
Jan 21 '09 #4

Expert 100+
P: 750
PRR
@santhugala
Service starting and stopping takes time... probably a few seconds at most ...
Expand|Select|Wrap|Line Numbers
  1. ServiceController con = new ServiceController("servicename");
  2. //"Windows Image Acquisition (WIA)");
  3.  
  4.  
  5.             con.Start();
  6.  
  7.             System.Threading.Thread.Sleep(5000);
  8.  
  9.             ServiceControllerStatus st=con.Status;           
  10.  
  11.             if (st == ServiceControllerStatus.Running)
  12.             {
  13.                 try
  14.                 {
  15.                     con.Stop();
  16.                 }
  17.                 catch (Exception ex)
  18.                 {
  19.                     Console.WriteLine(ex.Message.ToString());
  20.                 }
  21.             }
  22.  
Jan 21 '09 #5

P: 4
Thanks for ur information.

There is an overriden method to start the service and stop the service.

I am creating an object of servicecontroller() like

system.serviceprocess.servicecontroller objService = new servicecontroller(Service Name);

protected override void OnStart(String[] args)
{
objService.Stop(); // When i write like this , and open Services.msc and Select the service and click on "start the service". Then the
Service should not get started.But the service is started in my case.
------
---------
--------
}


And if i write
protected override void OnStart(String[] args)
{
objService.Stop();
objService.Stop(); // When i write like this , and open Services.msc and Select the service and click on "start the service". Then the
Service should not get started. The service is not started when i write 2 times .Stop() method.

--------
-----------
----------
}

This is the main problem iam facing.
Jan 21 '09 #6

Expert 100+
P: 750
PRR
are you trying to start and stop a service from within a service? If in serivce A code you write code to stop service B code.. You need to see if your service has rights to do so...
"system.serviceprocess.servicecontroller objService = new servicecontroller(Service Name);"

Also check the status of the service ... As i said earlier it takes time for a service to stop or start....

Are you by any chance tryin to stop the service itself? You need to check whether the service canstop... Also you certainly cant stop the executing service on onstart....
Expand|Select|Wrap|Line Numbers
  1. if (this.CanStop)
  2.             {
  3.                 this.Stop();
  4.             }
  5.  
Jan 21 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.