4.5. Execute a Method at a Specific Time

Problem

You need to execute a method in a separate thread at a specific time.

Solution

Declare a method containing the code you want to execute. The method's signature must match that defined by the System.Threading.TimerCallback delegate; that is, it must be a subroutine (not a function) and take a single Object argument. Create a System.Threading.Timer object, and pass it the method you want to execute along with a state Object that the timer will pass to your method when the timer expires. Calculate the time difference between the current time and the desired execution time, and configure the Timer object to fire once after this period of time.

How It Works

Executing a method at a particular time is ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.