Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Service Onstart OnStop

Posted on 2004-10-14
13
Medium Priority
?
969 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

609 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