Y
Yves Royer
Hi all,
I have a little question about Windows Services.
For my app i need 3 different Windows Services, each with its own
functionality. I'm trying to make a test service to see what happens when a
service is stopped or started.
What i'm trying do do now is adding a timer so after a specific period of
time the service does something, in the example below writing to a text
file.
Here is the code of my test services:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace WindowsTestService
{
public partial class WindowsTestService : ServiceBase
{
public WindowsTestService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("#----------#");
sw.WriteLine("Service started @ " + DateTime.Now);
this.timer1.Start();
this.timer1.Enabled = true;
sw.WriteLine("Timer enabled = " + this.timer1.Enabled);
sw.WriteLine("#----------#");
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
protected override void OnStop()
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("#----------#");
sw.WriteLine("Service stopped @ " + DateTime.Now);
this.timer1.Enabled = false;
this.timer1.Stop();
sw.WriteLine("Timer enabled = " + this.timer1.Enabled);
sw.WriteLine("#----------#");
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("Tick @ " + DateTime.Now);
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
}
}
After starting and stopping the service i only see this in the text file:
#----------#
Service started @ 14/08/2005 20:15:57
Timer enabled = True
#----------#
#----------#
Service stopped @ 14/08/2005 20:19:06
Timer enabled = False
#----------#
The target i'm aiming at is to have a service that does something after a
specific period of time. In this case the interval was set to 1500
miliseconds.
Am i doing in a wrong way or is there another approach...? The only
experience i have with Windows Services is the ServiceController component.
Creating an own Service is new to me.
Thanks in advance,
Yves
I have a little question about Windows Services.
For my app i need 3 different Windows Services, each with its own
functionality. I'm trying to make a test service to see what happens when a
service is stopped or started.
What i'm trying do do now is adding a timer so after a specific period of
time the service does something, in the example below writing to a text
file.
Here is the code of my test services:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace WindowsTestService
{
public partial class WindowsTestService : ServiceBase
{
public WindowsTestService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("#----------#");
sw.WriteLine("Service started @ " + DateTime.Now);
this.timer1.Start();
this.timer1.Enabled = true;
sw.WriteLine("Timer enabled = " + this.timer1.Enabled);
sw.WriteLine("#----------#");
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
protected override void OnStop()
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("#----------#");
sw.WriteLine("Service stopped @ " + DateTime.Now);
this.timer1.Enabled = false;
this.timer1.Stop();
sw.WriteLine("Timer enabled = " + this.timer1.Enabled);
sw.WriteLine("#----------#");
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine("Tick @ " + DateTime.Now);
}
catch (IOException)
{
}
finally
{
sw.Close();
fs.Close();
}
}
}
}
After starting and stopping the service i only see this in the text file:
#----------#
Service started @ 14/08/2005 20:15:57
Timer enabled = True
#----------#
#----------#
Service stopped @ 14/08/2005 20:19:06
Timer enabled = False
#----------#
The target i'm aiming at is to have a service that does something after a
specific period of time. In this case the interval was set to 1500
miliseconds.
Am i doing in a wrong way or is there another approach...? The only
experience i have with Windows Services is the ServiceController component.
Creating an own Service is new to me.
Thanks in advance,
Yves