Windows Service does not start

M

mpriem

Hi,

I am developing my very first Windows Service.
I want to query AD every 5 minutes to generate a Report which I will
use in a website.
I use the following testcode, but the service won't start in a timely
fashion. What am I doing wrong?

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

namespace RSP_MailboxCapacity
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.IO.StreamWriter objStream;
private System.IO.FileInfo objFile;
private bool bolStop = true;

public Service1()
{
// 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
Service1() };
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 = "RSP_MailboxCapacity";
}

/// <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)
{
this.Go();
// TODO: Add code here to start your service.
}

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

private void Go()
{
objFile = new FileInfo(@"c:\test.txt");
objStream = objFile.CreateText();
while(this.bolStop)
{
objStream.WriteLine(System.DateTime.Now);
objStream.Flush();
System.Threading.Thread.Sleep(2000);
}
}


}
}
 
W

Willy Denoyette [MVP]

mpriem said:
Hi,

I am developing my very first Windows Service.
I want to query AD every 5 minutes to generate a Report which I will
use in a website.
I use the following testcode, but the service won't start in a timely
fashion. What am I doing wrong?

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

namespace RSP_MailboxCapacity
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.IO.StreamWriter objStream;
private System.IO.FileInfo objFile;
private bool bolStop = true;

public Service1()
{
// 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
Service1() };
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 = "RSP_MailboxCapacity";
}

/// <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)
{
this.Go();
// TODO: Add code here to start your service.
}

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

private void Go()
{
objFile = new FileInfo(@"c:\test.txt");
objStream = objFile.CreateText();
while(this.bolStop)
{
objStream.WriteLine(System.DateTime.Now);
objStream.Flush();
System.Threading.Thread.Sleep(2000);
}
}


}
}


You should return from your Onstart method in a timely fashion (default 30
seconds), otherwise the SCM (Service Control Manager) will consider your
service as failed to start.
So what you should do is, initialize your service invariants (read config
parameters create resources etc..) and start a new thread to run your actual
service code.
When this has been done successfuly return from OnStart, if something failed
throw an exception and let your service die (a message will be logged into
the eventlog).

Willy.
 
M

Maqsood Ahmed

Hello,
Actually execution of OnStart method never ends. You have a while(true)
loop in Go() method, which never breaks and the windows thinks that the
service was unable to start. You can make a seperate thread to execute
Go Method.

<code>
System.Threading.Thread thread = new System.Threading.Thread(new
System.Threading.ThreadStart(Go));
thread.Start();
</code>

HTH. Cheers.
Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net
 

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