Nik's Technology Blog

Travels through programming, networks, and computers

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

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

 

Product Review: Train Signal's IIS Web Servers CBT Video Training

As an ASP.NET web developer, I think it's important to understand and know how to configure Microsoft's web server, Internet Information Services (IIS). Depending on the organisation you work for you may or may not get the opportunity to tinker with IIS, but this shouldn't stop you from learning the basics.

You could go out and buy a book on configuring IIS and then install IIS on your computer to practise what you've read, but thanks to the guys at Trainsignal.com who have kindly sent me some of their training videos, I've discovered a much easier way of learning.

Train Signal CD-ROM

Train Signal provides video training courses for Microsoft, Cisco and CompTIA certifications, including CCNA, A+, Network+.
I'll also be reviewing the Cisco CCNA training videos here soon.

Train Signal's IIS Web Server video training covers both IIS 5 and IIS6, and features topics including installing IIS, creating test websites, hosting more than one website using host headers, adding security, setting up an FTP server, and web server optimisation.

Train Signal CD-ROM menu

The course is taught by Scott Skinger, President and founder of Train Signal. Scott has many years of experience in the IT field, holds various IT certifications and is a competent instructor. The videos are easy to follow and Scott's narration is second to none.

Train Signal lab book sample

The series of videos are backed up with a written guide in the form of the lab book, which comes as a printable PDF on the CD ROM, this goes through the same steps featured in the videos and includes network diagrams like the one above to help you set-up your own lab.

Train Signal video player

If you want to get up to speed on a particular Microsoft product, obtain an IT certification or you don't like reading IT text books then I definitely recommend you give these training videos a try.

Course Contents in full:

Introduction
Lab Setup
Setting up the lab
Computer 1
Computer 2
Computer 3
Lab
Scenario
Installing IIS on Windows 2000 Server
Creating an HTML file
Hosting Ben & Brady's site
Configure DNS so Internet users can find your website
Testing the website from the client
Lab
Scenario
Creating a test website using an HTML file
Creating an additional website on the web server
Creating host headers
Configuring DNS for the second website
Test and view website from client
Assigning site operators
Adding security to a website
Test and view the website from a client
Lab
Scenario
Downloading and installing service packs and hot fixes
Setting NTFS permissions
Disabling Netbios over TCP/IP
Download and run The IIS lockdown tool from Microsoft
Enable and view logging

USB U3 Smart Drives: Drives That Make Your Applications & Data Portable

USB flash drives have increased in capacity in leaps and bounds since I last purchased one. In the few years since I bought a Crucial 128MB Gizmo!, the price of flash memory has been literally free-falling, due partly to economies of scale and the mass adoption of flash-based mp3 players.
The size and sheer variety of these devices is astounding, but what I wasn't expecting when I inserted the drive was for a Launchpad application to start running, pre-loaded with special software!

SanDisk u3 Smart Drive - Cruzer Micro 4GB

I had in fact purchased a 4GB SanDisk U3 Smart Drive. U3 is a technology developed by SanDisk which effectively creates a platform for developers to build applications that install directly onto the flash drive rather than the host computer. This means that not only can you take your data with you, but you can take your applications too!

U3 Smart Drive Launchpad

When you insert your U3 Smart Drive into a USB slot on any computer, the U3 Launchpad is loaded, which is effectively like the Window's start menu, but instead contains menus to configure the drive, run installed applications and access your data. Nothing is installed on the host PC, so you can take your applications and data with you and its all secure and synced with your data on your PC back home.

The software that's available includes Skype, Firefox, Opera, various password safes, Thunderbird, OpenOffice... The list goes on. Some applications are free, while others cost a small amount of money, but most have downloadable trials. Here's a full list of U3 software.

Watch the video below for a quick guide to the U3 Smart Drive technology.

Buying New PC Hardware? - Make Sure It Supports Vista!

It's fast approaching the anniversary of the release of Windows Vista to business users, home users have been buying new PCs with Vista pre-loaded since the end of January 2007.
I haven't upgraded to Vista yet; my DVD upgrade is still in its box. I've installed it a couple of times to have a play around with it, to see which pieces of hardware and software are compatible, but that's it.
There are a couple of reasons why I haven't taken the plunge. Firstly, my PC can run the new Aero UI on Home Premium, but when I add the CPU monitor widget to the desktop to see how well it copes, it tends to max my processor out just opening windows etc. This is probably to be expected with a 4 year old computer. The second reason is the hardware and software support for Vista. You would have thought that manufacturers would have started to factor in support for a new Microsoft operating system, wouldn't you?
The word that springs to mind when talking about Vista compatibility today is "patchy", and today is almost 12 months after the official launch!

Future Proof Your Hardware Purchases

Most people who buy hardware or software for their PC will be expecting it to work with Vista out-of-the-box. They don't want to be updating firmware, or worse still finding out that their new device only supports XP! Why does Microsoft bother having alpha and beta testing periods when the likes of Apple can't even make their flagship iTunes work?

My advise to anyone thinking of buying a new piece of hardware or software is to make sure it supports Vista, even if you're sticking to Windows XP for the foreseeable future. You never know when you might buy a new PC, and do you really want to have to replace your hardware once you've upgraded to Vista?

Windows Vista Upgrade Advisor and Hardware Incompatibility

Just to recap, I bought Windows XP Media Center edition with an upgrade voucher to Windows Vista Home Premium last year. After sending off all my details to claim my Vista DVD and receiving it, I thought it best to clear some room on one of my hard disks and run Windows Vista Upgrade Advisor before I go any further.
I had previously ran a beta version of the upgrade advisor prior to upgrading my OS from Windows XP Pro to Media Center edition and as far as I can remember all my hardware was compatible (software was another story!).

Problems!

However... While upgrading my machine to Media Center I also upgraded some of my hardware. I upgraded my wireless 802.11b Netgear MA111 USB stick to a US Robotics 5418 802.11g PCI adapter (This allows for video streaming around my house via my Xbox 360, and also reduces the amount of USB devices I have hanging off the back of my machine).
I was also severely running out of disk space, so I added a SATA RAID card and bought two SATA HDDs so I could set up a mirrored (RAID 1) configuration for a little peace of mind).

After running the upgrade advisor you can now probably guess what devices weren't supported in Windows Vista...Yep, the USR5418 wireless PCI adapter and the Adaptec 1210SA SATA RAID controller, typical I thought!

I have checked the manufacturers' support sections for these devices, but future Vista drivers for them don't look promising. So unless I can find a driver that will work with these devices I don't see much point in upgrading to Vista at the moment. Can you imagine the Aero interface in all it's glory, but no data to access and no web to surf on!! The only other solution is to upgrade these components again, but I am reluctant to spend anymore money on my aging computer.

Hardware Manufacturers or Microsoft?

So who is to blame for this lack of compatibility? The devices aren't exactly that old, Microsoft has had numerous beta versions of Vista available for hardware and software companies to test and develop for, but it seems they want you to buy the latest product instead. Even big companies like Apple had problems with iTunes after the Vista launch.

Where does Apple TV leave the Xbox 360?

Apple have just released Apple TV, essentially a device used to wirelessly bridge the gap between iTunes on your computer (Mac, PC) and your TV set in your living room.

Apple added video downloads to the iTunes service a while back, by getting several Hollywood studios on-board. These videos have so far only be available to watch on your computer screen or on your iPod video. Apple TV aims to fix this, by allowing your iTunes library to be wirelessly streamed to your HDTV.

Xbox 360's Media Capabilities

Microsoft on the other hand have similar strategies on invading peoples' living rooms with digital content, theirs though, is in the form of the Xbox 360, which as well as being a high definition games machine is also a very capable Media Center Extender.

As a Media Center Extender it wirelessly streams content from Windows Media Center, or Windows Vista (Home Premium and Ultimate editions) computers. If you don't have a Media Center edition of Windows you can still stream your music, pictures and just recently WMV videos too from the Xbox console using Windows Media Connect.

So How Do Apple TV and Xbox 360 Compare?

Ok, here's a matrix comparision table between Apple TV and the Xbox 360, so you can make your own mind up.

Feature
Apple TV
Xbox 360
HD Games Machine
No
Yes
Stream Audio
Yes
Yes
Stream Video
Yes
Yes
Stream Photos
Yes
Yes
Component Video
Yes
Yes
HDMI
Yes
No
Audio Out
Yes (optical and RCA analogue)
Yes (optical and RCA analogue)
Ethernet connection
Yes
Yes
Wireless capabilities
Yes (built-in) (pre-n)
Yes (sold separately) (a, b, g)
HD Resolution capabilities
1080i, 720p, 576p, or 480p 1080p, 1080i, 720p, 480i,480p
DVD player
No
Yes
HD DVD player
No
Yes (sold separately)
iTunes compatibility
Yes
No
Remote control
Yes
Yes (sold separately)
HD drive
40GB
20GB (premium package only, not needed for streaming)
Video formats supported
H.264 and protected H.264 (from iTunes Store): Up to 5 Mbps, Progressive Main Profile (CAVLC) with AAC-LC audio up to 160 Kbps (maximum resolution: 1280 by 720 pixels at 24 fps, 960 by 540 pixels at 30 fps

iTunes Store purchased video: 320 by 240 pixels or 640 by 480 pixels

MPEG-4: Up to 3 Mbps, Simple Profile with AAC-LC audio up to 160 Kbps (maximum resolution: 720 by 432 pixels at 30 fps)
WMV only, although a Windows 3rd party app called Transcode360 will allow most other formats to be streamed
Audio formats supported
AAC, protected AAC (from iTunes Store), MP3, MP3 VBR, Apple Lossless, AIFF, WAV MP3, WMA, un-encoded AAC, CD, DVD audio