Windows Services


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
 
Ad

Advertisements

B

Bennie Haelen

Hi Yves,

I might be missing something, but it looks like you never wired up your
timer event handler, like this:

timer.Tick +=new EventHandler(timer_Tick);

Of course, that might have been done in your "InitializeComponent()"
method, I am not sure, just thought I would check first..

Bennie Haelen

Yves said:
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
 
Y

Yves Royer

Indeed. I forgot to tell that i dropped a Timer component via the Toolbox
into the Design view of the Service. The Tick event is set via the Property
window.
Also the interval and things like that are done there and not in code.

Thanks for the help.

Yves


Bennie Haelen said:
Hi Yves,

I might be missing something, but it looks like you never wired up your
timer event handler, like this:

timer.Tick +=new EventHandler(timer_Tick);

Of course, that might have been done in your "InitializeComponent()"
method, I am not sure, just thought I would check first..

Bennie Haelen

Yves said:
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
 
Ad

Advertisements

Y

Yves Royer

O, i fund out what i did wrong. Using the timer component isn't wrong... you
only have to user the right one. The one i was using (via the Toolbox) was
the one of System.Windows.Forms which doesn't work in a Service
environment...

The correct one is the one of System.Timers. After changing this the timer
did his job.

Thanks for the efford.

Yves

Bennie Haelen said:
Hi Yves,

I might be missing something, but it looks like you never wired up your
timer event handler, like this:

timer.Tick +=new EventHandler(timer_Tick);

Of course, that might have been done in your "InitializeComponent()"
method, I am not sure, just thought I would check first..

Bennie Haelen

Yves said:
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
 

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