JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Sleeping Threads

You can put a thread to sleep for a number of milliseconds (1/100ths of a second) with the Sleep method:

NewThread.Sleep(NumberOfMilliseconds)

Putting a thread to sleep for a length of time simply makes it stop executing for that length of time. This is particularly useful if you want to give another thread access to a resource that a thread is using; you can just put the active thread to sleep for a while.

We saw how to use the Sleep method in the Threading example, as discussed in the In Depth section of this chapter. When the user clicks the "Sleep 10 Seconds" button, the code puts the thread it created to sleep for 10 seconds, like this (you can see this example at work in Figure 24.10):

    Dim Thread1 As New System.Threading.Thread(AddressOf counter1.Count)
        
    Private Sub Button3_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button3.Click
        Thread1.Sleep(10 * 1000)
    End Sub

For more on how this example functions, take a look at the discussion in the In Depth section of this chapter.

Tip 

As mentioned earlier in the chapter, although the time you pass to Sleep is measured in milliseconds, Windows machines can only measure time intervals with limited precision. In my tests, the smallest sleep interval I can get is a hundredth of a second, which means that calling Sleep(1) is the same as calling Sleep(10).

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor