Solved

Service Onstart OnStop

Posted on 2004-10-14
13
959 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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