By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Windows Service (Stopping and Starting)

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
CG
No

I start a thread in the elapsed event for my timer.
Jul 21 '05 #3

P: n/a
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

P: n/a
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

P: n/a
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.