O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use a Windows Service with a Thread

Problem

You want to create a Windows service that performs some type of long-running task continuously.

Solution

Create a new thread when the service is started, and perform all your work on that thread.

Discussion

As explained earlier, the OnStart method is used to set your Windows service processing in motion, but it can’t perform the work directly itself. One of the most common design patterns is to use the OnStart method to create and start a new thread. The OnStop method can then terminate this thread.

This thread can process continuously, using a loop if it needs to repeat the same work. In addition, you might want to use a Boolean member variable to allow the OnStop method to signal a polite stop (a technique ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required