473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

System.Threading.Timers - Callback doesn't happen

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
.....}
}
************************************************** ***********************
Nov 16 '05 #1
1 2254
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/

On 8 Oct 2004 14:15:26 -0700, To*@TheEnderles.org (Tom) wrote:
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
....}
}
************************************************* ************************


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
0
by: Kelsang Wangchuk | last post by:
Hi Just a quick question... When would you use System.Timers.Timer, and when System.Threading.Timer? What are the principal differences between them? There is a lot of discussion about...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
1
by: james.e.coleman | last post by:
Hello, System.Timers.Timer/System.Threading.Timer do not fire on my development server, yet they work perfectly on the production server. I would appreciate any help on what the cause of this...
1
by: logdenav | last post by:
Hello I'm testing the performance of the System.Timers.Timer class. I created a small program that create 100 User objects. Each USer object create a MyTimer object. The constructor of the User...
0
by: Bruce | last post by:
Hi I have a question on using System.Threading.Timer. I understand that in order for the timer to work, I need to store a reference to the timer so that it does not get garbage collected. In my...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.