469,950 Members | 1,997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Windows Service does not start

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(ServicesToRu n);
}

/// <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);
}
}
}
}

Nov 17 '05 #1
3 2358


"mpriem" <sp**@mpriem.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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(ServicesToRu n);
}

/// <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.
Nov 17 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Thanks Guys!!
It works perfectly now!!

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
3 posts views Thread by Doug Bailey | last post: by
4 posts views Thread by SQLScott | last post: by
2 posts views Thread by Tom Simpson | last post: by
5 posts views Thread by dm3281 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.