473,769 Members | 5,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem restarting a windows service with ServiceControll er

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 servicecontroll er 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.
InitializeCompo nent();
log = new EventLog("Appli cation", ".", "ServiceTes t");
_timer = new Timer(new TimerCallback(O nTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry( "Timer");
serviceControll er1.Stop();
log.WriteEntry( "Stopping") ;
serviceControll er1.WaitForStat us(System.Servi ceProcess.Servi ceControllerSta tus.Stopped);

log.WriteEntry( "starting") ;
serviceControll er1.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 9348
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 servicecontroll er 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.
InitializeCompo nent();
log = new EventLog("Appli cation", ".", "ServiceTes t");
_timer = new Timer(new TimerCallback(O nTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry( "Timer");
serviceControll er1.Stop();
log.WriteEntry( "Stopping") ;
serviceControll er1.WaitForStat us(System.Servi ceProcess.Servi ceControllerSta tus.Stopped);

log.WriteEntry( "starting") ;
serviceControll er1.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******** *************@d 34g2000cwd.goog legroups.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 servicecontroll er 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.
| InitializeCompo nent();
| log = new EventLog("Appli cation", ".", "ServiceTes t");
| _timer = new Timer(new TimerCallback(O nTimer), null, 20000,0);
| }
|
| private void OnTimer(object state)
| {
| try
| {
| log.WriteEntry( "Timer");
| serviceControll er1.Stop();
| log.WriteEntry( "Stopping") ;
|
serviceControll er1.WaitForStat us(System.Servi ceProcess.Servi ceControllerSta tus.Stopped);
|
| log.WriteEntry( "starting") ;
| serviceControll er1.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******** *************@d 34g2000cwd.goog legroups.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 servicecontroll er 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.
InitializeCompo nent();
log = new EventLog("Appli cation", ".", "ServiceTes t");
_timer = new Timer(new TimerCallback(O nTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry( "Timer");
serviceControll er1.Stop();
log.WriteEntry( "Stopping") ;
serviceControll er1.WaitForStat us(System.Servi ceProcess.Servi ceControllerSta tus.Stopped);

log.WriteEntry( "starting") ;
serviceControll er1.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.mach in AT dot.state.fl.us wrote
in message news:uX******** ******@TK2MSFTN GP02.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
serviceControll er1.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.mach in AT dot.state.fl.us wrote
in message news:uX******** ******@TK2MSFTN GP02.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
serviceControll er1.Start(); could ever be reached?

Willy.
Sep 26 '06 #6
Hi,

"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:uq******** ******@TK2MSFTN GP05.phx.gbl...
>
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote
in message news:uX******** ******@TK2MSFTN GP02.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
serviceControll er1.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
3502
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 be appreciated. Simply leaving the threadMain() method if something unexpected occurs during thread execution leaves the thread in a ThreadState.Running state. This (apparently??) causes the Thread.Join() in service.OnStop() to hang...
4
15895
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 : microsoft.public.dotnet.languages.csharp URL :...
8
1429
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 it's being called in the OnStart event (where I validate the parameters) and the service isn't fully running yet. How can I stop my service from within the service itself? Thanks. Jerry
3
3690
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 no problems. However, I am not able to start or stop the service unless I go through the process of impersonating an administrative user. (See MSDN KB 306158) Since it appears to be a privilege issue, I set the folders in IIS holding the Web...
13
5341
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 consequence my application can not work. I coult not catch or controle those exceptions, so my application needs to be able to restart my service when it does not answer animore. Does anyone knows how to do that? My application is in C#. I work with...
4
4182
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 check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it happens I can programmatically start the same service on another machine.
4
8832
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 how to get the service to exit when the worker thread has a fatal error *and* get the SCM to invoke the auto-restart configuration? The worker thread can error out while the parent thread in the server process chugs along. We can set the exit...
1
1728
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 I've seen. Most example services, if they encounter an error, simply stop processing but stay active. The route I've taken to avoid that is, on catching a fatal exception, I create a ServiceController instance and issue a Stop command. This
3
2597
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 Service, and Event Log tells... Event Type: Information Event Source: MsiInstaller Event Category: None Event ID: 11707
0
10215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.