Solved

Service Onstart OnStop

Posted on 2004-10-14
13
960 Views
Last Modified: 2008-03-10
I have created a service in C#

in the Onstart function
i would like to be able to stop the service if there is an error,
but the service does not stop.

Onstart()
{
    // code

    onstop()

}

How can I force the service to stop in Onstart?
0
Comment
Question by:doudou74
  • 6
  • 6
13 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 12311136
Just do an Application.Exit()

The reason it didn't stop is that the service per se had not yet been placed in the started mode.

OnStart() is what the OS triggers when the service's START command is issued.  The service is not considered started until its status is set to RUNNING.  While in OnStart(), c# keeps the status as PENDING until OnStart() is successfully exited.
0
 

Author Comment

by:doudou74
ID: 12316930
can i do an Application.Exit() in a service?? Is not is only for forms?
what should I include as a reference or namespace?

0
 
LVL 22

Expert Comment

by:cookre
ID: 12320500
Yes, that's what I do in my c# services - works fine.

using System.Windows.Forms;
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:doudou74
ID: 12321234
Onstart()
{
    // code

    Application.Exit()
}

I tried this but the service still appears as started in the list of services

0
 
LVL 22

Expert Comment

by:cookre
ID: 12321744
I would guess either there was an instance already running, or the OnStart() exited somewhere without hitting the Exit().

Take a look at the Application and System event logs to see if there is anything interesting there.
0
 

Author Comment

by:doudou74
ID: 12329207
i checked all this. it does not work
0
 
LVL 22

Expert Comment

by:cookre
ID: 12329309
Looks like you'll have to post the code...
0
 

Author Comment

by:doudou74
ID: 12336750
here is my code

protected override void OnStart(string[] args)
{
    // TODO: Add code here to start your service.

    Application.Exit()
}


protected override void OnStop()
{
  // TODO: Add code here to perform any   tear-down necessary to stop your service.
}
0
 

Author Comment

by:doudou74
ID: 12336887
here is my code

protected override void OnStart(string[] args)
{
    // TODO: Add code here to start your service.

    Application.Exit();
}


protected override void OnStop()
{
  // TODO: Add code here to perform any   tear-down necessary to stop your service.
}
0
 
LVL 22

Expert Comment

by:cookre
ID: 12353872
There are conditions under which .Exit() won't, such as hanging message boxes.

Surely there's more going on that just what was posted.
0
 
LVL 3

Expert Comment

by:mpf1748
ID: 12384394
There is no way to stop a process during the OnStart event. Create a global bool variable bError and set it to true if there is an error. Then start a timer on the very last line of code of the OnStart for say 5 milliseconds. In the event handler for the timer, check to see if bError is true. If so, Stop, else nothing. Be sure and disable the timer.

HTH,
Matt
0
 

Author Comment

by:doudou74
ID: 12384799
-what is the function to call to stop the service?
-can you give me an example of your idea of timer
0
 
LVL 22

Accepted Solution

by:
cookre earned 125 total points
ID: 12385267
// global
System.Timers.Timer INITimer;



protected override void OnStart(string[] args)
{
...
INITimer.Elapsed+=new ElapsedEventHandler(INITimer_Elapsed);
INITimer.Interval=1000; // or whatever
INITimer.Enabled=true;
}


void INITimer_Elapsed(object source,ElapsedEventArgs evt)
{
// Disable timer so we don't interrupt ourselves
INITimer.Enabled=false;

// Do things
...

}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 34
Service Controller 1 28
C# bracket error 3 36
.NET 2008 VB and C# 6 39
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question