Adding Service Functionality

The work of a service is typically done in a separate thread, using the thread function to do whatever the service is designed to do.


See the solution "Writing Multithreaded Code" for details on how to use threads.

When you create a thread, you nominate a function that is going to be run by the thread, and schedule it so that it runs in parallel with the rest of your application. The thread function that I'm going to use follows. Add it as a member of the class:

Sub DoBeep()

' This is the function that does the work of the service. ' It runs in a thread, and simply beeps every two seconds While bContinue = True Beep()

Thread.Sleep(2000) End While

End Sub

This is a very simple function. When this is run by a thread, the While loop will run, emitting a beep and then sleeping for two seconds, as long as the Boolean flag bContinue is set to True.

0 0

Post a comment