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