Some Services Stop automatically if they have nothing to do.......

C

Chucker

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top