Blog

Create a Simple Windows Service to Request a URL at Set Intervals

14. April 2009 13:45

I needed a simple Windows Service to request a web page at set intervals indefinitely. Windows Services are the best way of doing this as they have the ability to start automatically when the computer boots up and can be paused, stopped and restarted. You can also get them to write events to the Windows Event log.

I found this Windows Service sample tutorial on The Code Project and downloaded the code to familiarise myself with the basics. The tutorial lacked a timer and the code to request a URL though so I had to add this functionality.

Visual Studio Standard edition doesn't have a Windows Service template, but you can still create a Windows Service, you just need to do a bit of extra work.

After some research and a bit of coding I added two new methods:

private void ServiceTimer_Tick(object sender, ElapsedEventArgs e)
{
this.timer.Stop();
DoWork();
this.timer.Start();
}

void DoWork()
{
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(URL to request here);
client.Dispose();
data.Dispose();
}

I overrode the OnStart() method of ServiceBase to enable the timer and start it. I also overrode the OnStop() method to disable the timer.

The DoWork() method simply creates an instance of WebClient and reads in the URL you want to request.

Then in the constructor I set the timer interval and added an event handler to raise the ServiceTimer event when the interval elapses. The event handler simply stops the timer, calls the DoWork() method and then restarts the timer.

public static void Main()
{
ServiceBase.Run(new Service3());
}

public Service3()
{

InitializeComponent();
// Duration 1 hour
double interval = 3600000;

timer = new Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.ServiceTimer_Tick);

}

To install the Service you need to publish the project in Visual Studio. Then use InstallUtil.exe following the process below:

  1. Open a Visual Studio .NET Command Prompt
  2. Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode)
  3. Issue the command InstallUtil.exe MyWindowsService.exe to register the service and have it create the appropriate registry entries
  4. Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage
  5. In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services
  6. Start your service by right clicking on it and selecting Start

Each time you need to change your Windows Service it will require you to uninstall and reinstall the service. Prior to uninstalling the service make sure you close the Services management console. To uninstall the service simply reissue the same InstallUtil command used to register the service and add the /u command switch.

e.g. InstallUtil.exe /u MyWindowsService.exe

When you install the service on a server you can find the InstallUtil.exe in the .NET framework folder e.g. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

 

Tags: , , ,

Filed under: Windows | Software Development

Implementing UML 2.0 Use-Cases with Visio 2007

6. July 2007 13:26

I've been busy creating some use-cases in Visio 2007 for an upcoming project and from what I can gather either Microsoft has not implemented the current UML 2.0 standard and stuck with an older version, or it decided what parts of the specification to implement.

Either way it's a little annoying, I'm having to spend extra time working out what was the equivalent diagram or notation in UML 1.2 was in order to draw it in Visio.

An example of this would be where you link two Use-Cases with the «include» relationship. This doesn't seem to exist in Visio, so it looks like you have to make do with the «uses» stereotype instead, which was replaced in the current standard.

A few other Visio bug-bears would be the sparse Use-Case documentation interface. It doesn't allow rich text, so adding more than a few lines of text could become very hard to read. Microsoft also seem to leave it up to the modeller to format their own use-case specification; but with such a poor interface I can't see how you could use table-orientated steps to make a sequence of events more readable.

Surely allowing attachments (in the form of Microsoft Word) to be added would make more sense?

Tags: , ,

Filed under: Software Development