473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 2480


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I created an extremely simple windows service that only writes to the EventLogs on Stop and Pause. I installed it using the InstallUtil.exe program, the output of which is below. It appears to be...
3
by: Doug Bailey | last post by:
I am trying to control a Windows Service via a Web Service interface. (I am developing in .NET2003) I am using the ServiceController object which allows me to read the state of the services with...
3
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim...
4
by: SQLScott | last post by:
My esteemed VB.Net gurus, I posted this question a week or so ago and recieved 1 response that helped me somewhat but really did not solve the problem (but I do appreciate the response). I...
4
by: Blaxer | last post by:
I have read approximately 30 articles now on various methods to make your vb.net application automatically update itself and I can't see how they apply to a vb.net windows services projects. The...
2
by: Tom Simpson | last post by:
I have set up a Windows service in VB.NET, and created a 'Service Manager' Windows Forms app that can 'get' information from that service. The service consists of the basic service 'framework'...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
1
by: TJ | last post by:
I created a windows service using VS2005. I created/adjusted the install class using the designer controls. I set the user to be 'local system'. I installed the service using the VS2005 cmd...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.