473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem restarting a windows service with ServiceController

Hi Everybody,
i need to restart a windows service made with c# when an fixed event is
raised.
So to test it, I've added a servicecontroller to my service, and in the
creation method, i've created a new thread to try to stop it.
This is my code:

public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
log = new EventLog("Application", ".", "ServiceTest");
_timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry("Timer");
serviceController1.Stop();
log.WriteEntry("Stopping");
serviceController1.WaitForStatus(System.ServicePro cess.ServiceControllerStatus.Stopped);

log.WriteEntry("starting");
serviceController1.Start();
log.WriteEntry("Restarted");
}
catch (Exception ex)
{
log.WriteEntry(ex.ToString());
}

}

When service is stopped it doesn't start again, what's the problem? Can
you help me?

Thanks!

Sep 22 '06 #1
6 9318
The issue is when you service stops, it is stopped. The code is stopped and
therefore never calls the start command. You need to call some other program
to stop and resart the service.

Ciaran O'Donnell

"se************@gmail.com" wrote:
Hi Everybody,
i need to restart a windows service made with c# when an fixed event is
raised.
So to test it, I've added a servicecontroller to my service, and in the
creation method, i've created a new thread to try to stop it.
This is my code:

public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
log = new EventLog("Application", ".", "ServiceTest");
_timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry("Timer");
serviceController1.Stop();
log.WriteEntry("Stopping");
serviceController1.WaitForStatus(System.ServicePro cess.ServiceControllerStatus.Stopped);

log.WriteEntry("starting");
serviceController1.Start();
log.WriteEntry("Restarted");
}
catch (Exception ex)
{
log.WriteEntry(ex.ToString());
}

}

When service is stopped it doesn't start again, what's the problem? Can
you help me?

Thanks!

Sep 22 '06 #2
You can't control your service from within the same service.
You need another program to stop and start services.
I'm not clear on why you need to restart a service anyway, services are
invented to run from boot to shutdown time, and should only restart when a
catastrofic failure prevents the service to continue it's tasks. In that
case the service can be set-up to restart under control of the SCM.

Willy.


<se************@gmail.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
| Hi Everybody,
| i need to restart a windows service made with c# when an fixed event is
| raised.
| So to test it, I've added a servicecontroller to my service, and in the
| creation method, i've created a new thread to try to stop it.
| This is my code:
|
| public Service1()
| {
| // This call is required by the Windows.Forms Component Designer.
| InitializeComponent();
| log = new EventLog("Application", ".", "ServiceTest");
| _timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
| }
|
| private void OnTimer(object state)
| {
| try
| {
| log.WriteEntry("Timer");
| serviceController1.Stop();
| log.WriteEntry("Stopping");
|
serviceController1.WaitForStatus(System.ServicePro cess.ServiceControllerStatus.Stopped);
|
| log.WriteEntry("starting");
| serviceController1.Start();
| log.WriteEntry("Restarted");
| }
| catch (Exception ex)
| {
| log.WriteEntry(ex.ToString());
| }
|
| }
|
| When service is stopped it doesn't start again, what's the problem? Can
| you help me?
|
| Thanks!
|
Sep 22 '06 #3
Hi,

Do you get any error?

Does the service do logging?

I have a similar situation, I have a service that create a TCP Listener in a
worker thread of course, if you stop and restart it right away the worker
thread (or at least the port ) is still active and you get error.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<se************@gmail.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
Hi Everybody,
i need to restart a windows service made with c# when an fixed event is
raised.
So to test it, I've added a servicecontroller to my service, and in the
creation method, i've created a new thread to try to stop it.
This is my code:

public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
log = new EventLog("Application", ".", "ServiceTest");
_timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry("Timer");
serviceController1.Stop();
log.WriteEntry("Stopping");
serviceController1.WaitForStatus(System.ServicePro cess.ServiceControllerStatus.Stopped);

log.WriteEntry("starting");
serviceController1.Start();
log.WriteEntry("Restarted");
}
catch (Exception ex)
{
log.WriteEntry(ex.ToString());
}

}

When service is stopped it doesn't start again, what's the problem? Can
you help me?

Thanks!

Sep 22 '06 #4

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| Hi,
|
| Do you get any error?
|
| Does the service do logging?
|
| I have a similar situation, I have a service that create a TCP Listener in
a
| worker thread of course, if you stop and restart it right away the worker
| thread (or at least the port ) is still active and you get error.
|
|

You don't do the same thing as the OP I guess, you can't stop or start your
own service using the service controller. How would you think
serviceController1.Start(); could ever be reached?

Willy.


Sep 22 '06 #5
Thank you everybody,
Last day at late hour i get the same conclusion :( , a service cannot
be restarted himself.
Thx

Willy Denoyette [MVP] ha escrito:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| Hi,
|
| Do you get any error?
|
| Does the service do logging?
|
| I have a similar situation, I have a service that create a TCP Listener in
a
| worker thread of course, if you stop and restart it right away the worker
| thread (or at least the port ) is still active and you get error.
|
|

You don't do the same thing as the OP I guess, you can't stop or start your
own service using the service controller. How would you think
serviceController1.Start(); could ever be reached?

Willy.
Sep 26 '06 #6
Hi,

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uq**************@TK2MSFTNGP05.phx.gbl...
>
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| Hi,
|
| Do you get any error?
|
| Does the service do logging?
|
| I have a similar situation, I have a service that create a TCP Listener
in
a
| worker thread of course, if you stop and restart it right away the
worker
| thread (or at least the port ) is still active and you get error.
|
|

You don't do the same thing as the OP I guess, you can't stop or start
your
own service using the service controller. How would you think
serviceController1.Start(); could ever be reached?
Hi,

Not at all, I did not read the OP correctly, I assumed he meant from another
app, (in my case it's the user manually )
Sep 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David | last post by:
I've written a small windows service, and I'm having a problem that I'm spending a lot more time on than I'd like. If anyone has experienced this problem and has any hints or solutions; they would...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
8
by: Jerry Camel | last post by:
I want my service to terminate automatically if the specified parameters are invalid. I tried to use a servicecontroller component to attach to the service, but I think that it's failing because...
3
by: Doug Bailey | last post by:
I am trying to control a Windows Service via a Web Service interface. (I am developing in .NET2003) I am using the ServiceController object which allows me to read the state of the services with...
13
by: ghighi | last post by:
Hello, I am working on a .Net application (aspx+webservices) that uses a windows service I created too. Sometimes, the exe linked to my service crashes (mostly runtime exception), and as a...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
4
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... Following the samples online, we implemented a windows service in .Net. Like most services, there's a worker thread actually doing stuff in the background. The problem we've run into is...
1
by: sexauthor | last post by:
I've written a windows service am unsure how we're meant to handle fatal errors once the service is already up and running. It also doesn't seem to be mentioned in any of the books or examples...
3
by: Mika M | last post by:
I'm programming quite simple Windows Service using C# 2005. I also made setup for it into same solution. When I run setup to install service, it's going fine into C:\Program Files\MyCompany\My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.