Recipe 14.8. Pausing Execution of a Program


Problem

You want to postpone all activities on the current process thread.

Solution

Sample code folder: Chapter 14\PauseExecution

Put the thread to sleep using the System.Threading.Thread.Sleep() method. This method accepts an amount of time to "sleep," in milliseconds.

Discussion

Create a new Windows Forms application, and add a Button control named Button1. Now add the following code to the form's class template:

 Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click    Threading.Thread.Sleep(3000)    MsgBox("Good Morning") End Sub 

When you run the program and click on Button1, the "Good Morning" message appears after a three-second pause.

If your program includes only a single thread (the default behavior), putting the thread to sleep puts the entire application to sleep.

If you pass zero (0) to the Sleep() method, the thread pauses temporarily to allow other busy threads to perform some processing.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net