473,785 Members | 3,285 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 9350
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
1430
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
3691
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
5342
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
4184
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
1730
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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
10147
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
10085
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,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5379
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...
1
4045
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

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.