<begin possibly complicated email - watch for wrap in code sections>
There are several ways to do this. One with a single thread is [I cut out
most of my comments]:
// in Service1.cs (I rename mine, but I am using the default
// naming convention
// main service thread
private Thread svcThread;
#region Events
protected override void OnStart(string[] args)
{
this.Start();
}
protected override void OnStop()
{
this.Stop();
}
protected override void OnShutdown()
{
this.Stop();
}
#endregion
#region Private Methods
private void Start()
{
ServiceMain svcMain = new ServiceMain();
this.svcThread = new Thread(new ThreadStart(svcMain.Startup));
this.svcThread.Start();
}
private void Stop()
{
// check to see if the thread is Sleeping or waiting and stop it
if (this.svcThread.ThreadState == ThreadState.WaitSleepJoin)
this.svcThread.Interrupt();
// this throws an Abort on the worker thread
this.svcThread.Abort();
// joins
this.svcThread.Join();
this.svcThread = null;
}
#endregion
Then, I have a separate class (I am referring to is as ServiceMain as above
but you can use whatever you like). I have a single public method called
public void Startup() which is what I use to start my service code. I
normally don't use the constructor in this class because it can cause
unwanted side effects. Use it at your own discretion.
public void Startup()
{
// call start routines here
// service loop - this is infinite
// this is also what allows your service to restart
// if errors occur
do
{
try
{
// this is internal function I call to actually
// execute my processing. In a function call inside it
// I use Thread.Sleep() to pause the service between intervals
this.ExecuteProcess();
}
catch(ThreadAbortException e)
{
// must reset the abort in this thread to be able to continue
Thread.ResetAbort();
// write message to Event Log describing the thread abort
EventLog.WriteEntry(
Configuration.ApplicationName
, System.String.Format("Shutting down service: {0}",
e.Message)
, EventLogEntryType.Information
, 0);
// break out of the infinite do/while loop
break;
}
catch (ThreadInterruptedException e)
{
// Write a message to Event Log describing the thread innterrupt
EventLog.WriteEntry(
Configuration.ApplicationName
, System.String.Format("Shutting down service: {0}",
e.Message)
, EventLogEntryType.Information
, 0);
break;
}
// this is the global catch. You will want to have certain
exceptions
// added here to more finely control what stops your service
// and what you want to allow to continue your service.
// I have another class which allow ten fatal errors to occur then
// the service stops completely. In my case, the number of fatal
errors
// is controlled by a config file setting.
catch(Exception e)
{
EventLog.WriteEntry(
Configuration.ApplicationName
, System.String.Format(
"Error occured: {0}\n\nStack Trace: {1}"
, e.Message
, e.StackTrace)
, EventLogEntryType.Error
, 0);
if (ErrorManager.ErrorState.ExitError ==
ErrorManager.CheckErrState(
Configuration.ApplicationName
, Configuration.WaitTimeForError
, Configuration.ExitErrorCount))
break; // Exit the processing loop
}
}
while (1 == 1);
// handle shutdown processing
this.Shutdown();
}
David L. Penton, Microsoft MVP
JCPenney Technical Specialist / Lead
"Mathematics is music for the mind, and Music is Mathematics for the
Soul. - J.S. Bach"
[EMAIL PROTECTED]
Do you have the VBScript Docs or SQL BOL installed? If not, why not?
VBScript Docs: http://www.davidpenton.com/vbscript
SQL BOL: http://www.davidpenton.com/sqlbol
-----Original Message-----
From: Glenn Frankel [mailto:[EMAIL PROTECTED]]
I have a Windows Service that I have created in C# which calls a DB every
few seconds and then performs some processing. At times, I receive a DB
timeout or an exception when processing. I have attempted to handle all
exceptions, but if one goes through I want to terminate and let the
Windows restart the service. I have attemped to throw the exception is
high as I could, but the service would never restart. Does anyone know to
accomplish this?
---
You are currently subscribed to dotnet as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
---------
Administrated by 15 Seconds : http://www.15Seconds.com
List Archives/Search : http://local.15Seconds.com/search
Subscription Information : http://www.15seconds.com/listserv.htm
Advertising Information: http://www.internet.com/mediakit/