System.Threading.Timers - Callback doesn't happen

T

Tom

I've googled, and read, and stripped out code, and rewritten code, and
still can't get a System.Threading.Timer to work. (I hereby publicly
admit that I'm a failure here...) Could someone please take a quick
look at this and tell me where I'm going wrong? My actual use is more
complex, but when I couldn't get that to work I created a new service
project and figured to get a simple threading timer going first then
I'd revamp my actual code to fit. BUT... I find I'm not getting even
something this simple to fire correctly. The code below compiles and
installs as a service, runs fine... for the 1st event and 6 subsequent
callbacks (yes 6 and only 6, and this doesn't seem to vary)... then
quits. Aaaaaaaaaaaargh!

Any and all help is appreciated,

Tom



******************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Threading;


namespace TargetCheck
{
.....public class TargetCheck : System.ServiceProcess.ServiceBase
.....{
.........private System.ComponentModel.Container components = null;

.........public TargetCheck()
.........{
.............InitializeComponent();
.........}


.........static void Main()
.........{
.............System.ServiceProcess.ServiceBase[] ServicesToRun;
.............ServicesToRun = new System.ServiceProcess.ServiceBase[]
{new TargetCheck()};
.............System.ServiceProcess.ServiceBase.Run(ServicesToRun);
.........}


.........private void InitializeComponent()
.........{
.............this.ServiceName = "TargetCheck";
.........}

.........protected override void Dispose( bool disposing )
.........{
.............if( disposing )
.............{
.................if (components != null)
.................{
.....................components.Dispose();
.................}
.............}
.............base.Dispose( disposing );
.........}



.........protected override void OnStart(string[] args)
.........{
.............Target t=new Target("C:\\temp\\result.txt");....
.............System.Threading.Timer stt=new System.Threading.Timer(new
TimerCallback(t.GetTarget), null, 0,3000);
.........}

.........protected override void OnStop()
.........{
.........}


.........// Target Class
.........private class Target
.........{
.............internal string filename;

.............// Constructors
.............internal Target(string f)
.............{
.................filename=f;
.............}


.............// Methods
.............internal void GetTarget(object stateInfo)
.............{
.................CreateLogfile();
.................WriteLog("Target " + DateTime.Now);
.............} // End GetTarget


.............private void CreateLogfile()
.............{
.................if (!(File.Exists(filename)))
.................{
.....................FileStream fs = File.Create(filename);
.....................fs.Close();
.................}
.............} // End CreateLog


.............private void WriteLog(string s)
.............{
.................StreamWriter fOut = File.AppendText(filename);
.................lock(fOut)
.................{
.....................fOut.WriteLine(s);
.....................fOut.Close();
.................}
.............} // End WriteLog
.........}
.........//End Target Class
.....}
}
*************************************************************************
 
S

Scott Allen

Hi Tom:

I imagine you are eventually running into an IO exception as multiple
writers try to open the file for writing.

In the following:

StreamWriter fOut = File.AppendText(filename);
lock(fOut)

the code is trying to open the file for writing before getting the
lock (and each thread would be locking on a different object - in
effect not synchronizing the threads at all).

Consider a WriteLog method like:

static object logFileLock = new object();
private void WriteLog(string s)
{
lock(logFileLock)
{
using(StreamWriter fOut = File.AppendText(filename))
{
fOut.WriteLine(s);
}
}
}

This will block other writers with an effective lock and also make
sure the StreamWriter is properly closed even if an exception occurs.

HTH,

--
Scott
http://www.OdeToCode.com/

I've googled, and read, and stripped out code, and rewritten code, and
still can't get a System.Threading.Timer to work. (I hereby publicly
admit that I'm a failure here...) Could someone please take a quick
look at this and tell me where I'm going wrong? My actual use is more
complex, but when I couldn't get that to work I created a new service
project and figured to get a simple threading timer going first then
I'd revamp my actual code to fit. BUT... I find I'm not getting even
something this simple to fire correctly. The code below compiles and
installs as a service, runs fine... for the 1st event and 6 subsequent
callbacks (yes 6 and only 6, and this doesn't seem to vary)... then
quits. Aaaaaaaaaaaargh!

Any and all help is appreciated,

Tom



******************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Threading;


namespace TargetCheck
{
....public class TargetCheck : System.ServiceProcess.ServiceBase
....{
........private System.ComponentModel.Container components = null;

........public TargetCheck()
........{
............InitializeComponent();
........}


........static void Main()
........{
............System.ServiceProcess.ServiceBase[] ServicesToRun;
............ServicesToRun = new System.ServiceProcess.ServiceBase[]
{new TargetCheck()};
............System.ServiceProcess.ServiceBase.Run(ServicesToRun);
........}


........private void InitializeComponent()
........{
............this.ServiceName = "TargetCheck";
........}

........protected override void Dispose( bool disposing )
........{
............if( disposing )
............{
................if (components != null)
................{
....................components.Dispose();
................}
............}
............base.Dispose( disposing );
........}



........protected override void OnStart(string[] args)
........{
............Target t=new Target("C:\\temp\\result.txt");....
............System.Threading.Timer stt=new System.Threading.Timer(new
TimerCallback(t.GetTarget), null, 0,3000);
........}

........protected override void OnStop()
........{
........}


........// Target Class
........private class Target
........{
............internal string filename;

............// Constructors
............internal Target(string f)
............{
................filename=f;
............}


............// Methods
............internal void GetTarget(object stateInfo)
............{
................CreateLogfile();
................WriteLog("Target " + DateTime.Now);
............} // End GetTarget


............private void CreateLogfile()
............{
................if (!(File.Exists(filename)))
................{
....................FileStream fs = File.Create(filename);
....................fs.Close();
................}
............} // End CreateLog


............private void WriteLog(string s)
............{
................StreamWriter fOut = File.AppendText(filename);
................lock(fOut)
................{
....................fOut.WriteLine(s);
....................fOut.Close();
................}
............} // End WriteLog
........}
........//End Target Class
....}
}
*************************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top