How is the start sequence for windows service

T

TonyJ

Hello!!
Below I have written the main method, the C-tor and the OnStart.
Who is calling OnStart? It must be the operating system that calls it after
the C-tor has finished

1. Main is always started first
2. The C-tor is then called
3. The operating system is then calling OnStart


Here is my main for the service project.
*****************************
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Here is the C-tor
*************
public MyNewService()
{
InitializeComponent();
if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
"DoDyLog");

eventLog1.Source = "DoDyLogSourse";
// the event log source by which
// the application is registered on the computer

eventLog1.Log = "DoDyLog";
}

Here I have the OnStart
******************
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
}
 
C

Christof Nordiek

TonyJ said:
Hello!!
Below I have written the main method, the C-tor and the OnStart.
Who is calling OnStart? It must be the operating system that calls it
after
the C-tor has finished

1. Main is always started first
2. The C-tor is then called
3. The operating system is then calling OnStart

1. When the process is started, the Main method is run (like with every
exe).
2. The creator of the service class is called by the Main method (as you can
see in the code).

3. When the service is started, the OnStart method is called. This will
occur after then Main method has run.

The first 2 calls are down immediatly before the second, if the process was
started on behalf of that service. But it will not be repeated if the
process is allready running with another process.

You can debug the last call, by putting another service in the same
executable:
- Start the other service.
- Hook the debugger to the process.
- Set a breakpoint in the OnStart method (can be done earlier)
- Start the service.
Here is my main for the service project.
*****************************
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Here is the C-tor
*************
public MyNewService()
{
InitializeComponent();
if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
"DoDyLog");

eventLog1.Source = "DoDyLogSourse";
// the event log source by which
// the application is registered on the computer

eventLog1.Log = "DoDyLog";
}

Here I have the OnStart
******************
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
}
 
T

TonyJ

Hello!

What do you mean here. I understand some of it but not all.
You can debug the last call, by putting another service in the same
executable:
Start the other service.
Hook the debugger to the process.
Set a breakpoint in the OnStart method (can be done earlier)
Start the service.

I have done the following
I have added another service in the project by using add new item and then
choose window service
from the template.

What else do I need if I want to be able to debug the OnStart?

//Tony

Christof Nordiek said:
TonyJ said:
Hello!!
Below I have written the main method, the C-tor and the OnStart.
Who is calling OnStart? It must be the operating system that calls it
after
the C-tor has finished

1. Main is always started first
2. The C-tor is then called
3. The operating system is then calling OnStart

1. When the process is started, the Main method is run (like with every
exe).
2. The creator of the service class is called by the Main method (as you can
see in the code).

3. When the service is started, the OnStart method is called. This will
occur after then Main method has run.

The first 2 calls are down immediatly before the second, if the process was
started on behalf of that service. But it will not be repeated if the
process is allready running with another process.

You can debug the last call, by putting another service in the same
executable:
- Start the other service.
- Hook the debugger to the process.
- Set a breakpoint in the OnStart method (can be done earlier)
- Start the service.
Here is my main for the service project.
*****************************
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Here is the C-tor
*************
public MyNewService()
{
InitializeComponent();
if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
"DoDyLog");

eventLog1.Source = "DoDyLogSourse";
// the event log source by which
// the application is registered on the computer

eventLog1.Log = "DoDyLog";
}

Here I have the OnStart
******************
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top