467,921 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Windows Service (Stopping and Starting)

CG
Hi

I have developed a Windows Service

When I try to start the Service it tells me that it cannot start as there may not be any work to do
When I comment out below line of code in my OnStart event....
objEventLog.EnableRaisingEvents = Tru
....It starts fin

I have the same issue when stopping the service because of below line of code in my OnStop even
objEventLog.EnableRaisingEvents = Fals

I am using .NET 2003 and I have declared objEventLog as..
Friend WithEvents objEventLog As System.Diagnostics.EventLo

Does anyone have any idea as to why this is causing an issue when starting and stopping the service

Thanks
C

Jul 21 '05 #1
  • viewed: 2130
Share:
5 Replies
I may be out of topic. However, do you start a treat in your OnStart()
method so as to perform your real work?

By the way there is a nice article about Windows Service at:

http://www.dotnetcoders.com/web/lear...sservices.aspx

Josť
"CG" <an*******@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Hi,

I have developed a Windows Service.

When I try to start the Service it tells me that it cannot start as there may not be any work to do. When I comment out below line of code in my OnStart event....
objEventLog.EnableRaisingEvents = True
...It starts fine

I have the same issue when stopping the service because of below line of code in my OnStop event objEventLog.EnableRaisingEvents = False

I am using .NET 2003 and I have declared objEventLog as...
Friend WithEvents objEventLog As System.Diagnostics.EventLog

Does anyone have any idea as to why this is causing an issue when starting and stopping the service?
Thanks,
CG

Jul 21 '05 #2
CG
No

I start a thread in the elapsed event for my timer.
Jul 21 '05 #3
Personnaly, I would go this way:

**In your Service Class:

protected override void OnStart(string[] args)
{
// Define working directory (For a service, this is set to System)
Process pc = Process.GetCurrentProcess();
Directory.SetCurrentDirectory (pc.MainModule.FileName.Substring
(0,pc.MainModule.FileName.LastIndexOf(@"\")));
// Start the Worker thread
Thread WorkerThread = new Thread(new ThreadStart(m_Worker.DoWork));
WorkerThread.Start();
}
protected override void OnStop()
{
// Ask the Reception thread to stop receiving
m_Worker.Stop();
}

....
private Worker m_Worker;

*****
And then have a Worker class where you do your stuff.
The DoWork() will be the code to execute in your service. It will wait for
a termination request provided by the Service itself through Worker.Stop()
which can
either set an event flag or a local flag to true.
eg:
public void Stop()
{
// In case the thread is running, ask to stop and wait for cleanup
m_bMustStop = true;

if (m_Thread != null)
{
// Wait up to 25 s for the communication to terminate
if (!m_Thread.Join(25000))
{
// Thread was not able to terminate within given time
}
}
}

public void DoWork()
{
// Initialize some thread parameters
m_Thread = Thread.CurrentThread;
m_Thread.Name = "Whatever you want;
...
while (!m_bMustStop)
{

// do your stuff

// Sleep a while (eg. 10s)
Thread.Sleep(10000);
}
}

private Thread m_Thread = null;
"CG" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
No.

I start a thread in the elapsed event for my timer.

Jul 21 '05 #4
On Thu, 26 Feb 2004 13:26:50 +0100, Josť Joye wrote:
Personnaly, I would go this way: ... excelent demo ...


i just have to say that this excelent post has made it to my code archive -
thanks heaps
--
---------------------------------------------------------
Steven H - B.I.T. Otago Polytechnic, Dunedin, New Zealand
..net Geek
Jul 21 '05 #5
Thanks :-))

Josť
"Steven H" <sm***********@bigfoot.com - wont work anyway ;)> wrote in
message news:iq******************************@40tude.net.. .
On Thu, 26 Feb 2004 13:26:50 +0100, Josť Joye wrote:
Personnaly, I would go this way:
... excelent demo ...


i just have to say that this excelent post has made it to my code

archive - thanks heaps
--
---------------------------------------------------------
Steven H - B.I.T. Otago Polytechnic, Dunedin, New Zealand
.net Geek

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by SP | last post: by
reply views Thread by tshad | last post: by
1 post views Thread by Scott Davies | last post: by
4 posts views Thread by ZQ | last post: by
6 posts views Thread by D | last post: by
7 posts views Thread by Giulio Petrucci | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.