Some services stop automatically if they have no work to do, for e

G

Guest

I got a Server Class in an executable and I would like to change this in a
windows service. The code from the main function of the executable is the
following:

LoadReportingServer server = new LoadReportingServer ();
server->Start ();

Console::WriteLine (S"Press ENTER to stop the server...");
Console::ReadLine ();

server->Stop ();
server = 0;

I tried to turn this into a service following two tutorials, and it compiles
fine. Whenever I start the Service in the Service Manager, I get the Message
that my service started and stopped again.

Attempt 1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
server.start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

Attempt 2:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private ThreadStart threadDelegate;
private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.CanPauseAndContinue = true;
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
threadDelegate = new ThreadStart(server.Start);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

What am I doing wrong? Can anybody help me? Thanks in advance

Chucker
 
P

Phil Wilson

I've noticed that if you start a Service that stops pretty quickly, Windows
will tell you that there might be a problem (a message box) because it
stopped rather "quickly". I don't know what that time actually is, but if
you stay running longer, does that message go away? I made my Service run
for at least 30 seconds before it stopped itself, that cured it.
--
Phil Wilson [MVP Windows Installer]
----
Chucker said:
I got a Server Class in an executable and I would like to change this in a
windows service. The code from the main function of the executable is the
following:

LoadReportingServer server = new LoadReportingServer ();
server->Start ();

Console::WriteLine (S"Press ENTER to stop the server...");
Console::ReadLine ();

server->Stop ();
server = 0;

I tried to turn this into a service following two tutorials, and it
compiles
fine. Whenever I start the Service in the Service Manager, I get the
Message
that my service started and stopped again.

Attempt 1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
server.start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

Attempt 2:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private ThreadStart threadDelegate;
private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.CanPauseAndContinue = true;
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
threadDelegate = new ThreadStart(server.Start);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

What am I doing wrong? Can anybody help me? Thanks in advance

Chucker
 
L

Luc E. Mistiaen

maybe what you do actually works and it is your LoadReportingServer that
finishes prematurely because it is running in an unexpected environment (no
windows, no network, no user environment variables etc...)

To track service start problem, it might be good to use the eventlog to
trace all the major start steps (at least when you are in debug mode). This
is guaranteed to work and leave a track...

/LM

Chucker said:
I got a Server Class in an executable and I would like to change this in a
windows service. The code from the main function of the executable is the
following:

LoadReportingServer server = new LoadReportingServer ();
server->Start ();

Console::WriteLine (S"Press ENTER to stop the server...");
Console::ReadLine ();

server->Stop ();
server = 0;

I tried to turn this into a service following two tutorials, and it
compiles
fine. Whenever I start the Service in the Service Manager, I get the
Message
that my service started and stopped again.

Attempt 1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
server.start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

Attempt 2:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private ThreadStart threadDelegate;
private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.CanPauseAndContinue = true;
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
threadDelegate = new ThreadStart(server.Start);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

What am I doing wrong? Can anybody help me? Thanks in advance

Chucker
 
G

Guest

I have written a number of windows services and here's how I handle keeping
the service alive. I typically have a separate class like you have which
handles the workload for the service. I usually create a start and stop
method within that class which will handle initialization tasks not done
within the constructor and free up any resources at shutdown. Then I have an
entry point within that class which is the main method and it has a while
loop in it as follows:

while(_run){
//do stuff
}

In your start method you set _run to true and in your stop method you set it
to false so that method exits. All you have in the actual service codes is
the instantiation of your object in the OnStart() along with calling the
start method of your custom object. Then in the OnStop() in the service you
call the stop method of your custom object.

In your console app, it's being kept alive because it's waiting for user
input. A service is simply going to run through the commands you have in the
OnStart and once it does those it will exit if there is nothing more to do.
That is why I use the while loop in the custom object mentioned above.



Luc E. Mistiaen said:
maybe what you do actually works and it is your LoadReportingServer that
finishes prematurely because it is running in an unexpected environment (no
windows, no network, no user environment variables etc...)

To track service start problem, it might be good to use the eventlog to
trace all the major start steps (at least when you are in debug mode). This
is guaranteed to work and leave a track...

/LM

Chucker said:
I got a Server Class in an executable and I would like to change this in a
windows service. The code from the main function of the executable is the
following:

LoadReportingServer server = new LoadReportingServer ();
server->Start ();

Console::WriteLine (S"Press ENTER to stop the server...");
Console::ReadLine ();

server->Stop ();
server = 0;

I tried to turn this into a service following two tutorials, and it
compiles
fine. Whenever I start the Service in the Service Manager, I get the
Message
that my service started and stopped again.

Attempt 1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
server.start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

Attempt 2:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace SoftwareLoadBalancing
{

public class LoadReportingService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private ThreadStart threadDelegate;
private LoadReportingServer server;

public LoadReportingService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
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
LoadReportingService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.CanPauseAndContinue = true;
this.ServiceName = "LoadReportingService";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>

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

server = new LoadReportingServer();
threadDelegate = new ThreadStart(server.Start);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
server.Stop ();
server = null;
}
}
}

This does not do anything but displaying the message that it started and
stopped again.

What am I doing wrong? Can anybody help me? Thanks in advance

Chucker
 

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