I am new to writing a windows service, could you please have a look at the following code so you can tell me why the timer_tick event never fires?
Thanks :-)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace MyNewService
{
public partial class MyNewService : ServiceBase
{
TextWriter tw = new StreamWriter("log.txt");
public MyNewService()
{
InitializeComponent();
tw.WriteLine(DateTime.Now + " COSHH Service has been installed correctly!");
tw.Close();
}
protected override void OnStart(string[] args)
{
timer1.Start();
}
protected override void OnStop()
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
tw.WriteLine(DateTime.Now + " hello from COSHH Updater!");
tw.Close();
}
}
}
Loading

Jeff Bolton8 reputationPosted Feb 14, 2009, 7:46 AM EST
Hi Matt. The problem is that you are trying to write to a closed TextWriter. You need to re-open the file, write to it, then close it.
Debugging windows services can be a real pain. It's very difficult to see what's happening internally, especially once you start adding multiple threads. I add logging everywhere so I have a step by step record of what's going on. I've also gotten in the habit of adding try/catch blocks to almost every section of my services. In the catch blocks, I once again add logging so I can see what happened. It takes a little more time to add all of the extra code, but it will save you tons of time in the long run.
Hope that helps!
Jeff
Matt3 reputationPosted Feb 14, 2009, 8:06 AM EST
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace MyNewService
{
public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();
TextWriter tw = new StreamWriter("log.txt");
tw.WriteLine(DateTime.Now + " COSHH Service has been installed correctly!");
tw.Close();
}
protected override void OnStart(string[] args)
{
timer1.Start();
TextWriter tw = new StreamWriter("log.txt");
tw.WriteLine(DateTime.Now + " COSHH Service has started up");
tw.Close();
}
protected override void OnStop()
{
timer1.Stop();
TextWriter tw = new StreamWriter("log.txt");
tw.WriteLine(DateTime.Now + " COSHH Service has stopped!");
tw.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("log.txt");
tw.WriteLine(DateTime.Now + " hello from COSHH Updater!");
tw.Close();
}
}
}
However I'm finding that ONLY timer_tick fires now
Len La Sala20 reputationPosted Feb 14, 2009, 11:32 AM EST
common.writeToFile("your message"); in the places you want to log.
As to your other problem,
Where is Timer1 declared? have you placed a timer compoent on a form or is it purly in code
have you tried to start timer just after the InitializeComponent
Timer t = new Timer();
public MyNewService()
{
InitializeComponent();
t.Start();
common.writeToFile("your message");
}
//write to log file function -------------------------------
internal static void writeToFile(string outText)
{
string path = @"c:\logs\myservice\trace.txt"; // your path and file name
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
outText += Environment.NewLine;
File.WriteAllText(path, outText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
outText += Environment.NewLine;
File.AppendAllText(path, outText, Encoding.UTF8);
}