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
.....}
}
*************************************************************************
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
.....}
}
*************************************************************************