Visual Basic Thread Sleep

In visual basic, Sleep method is useful to suspend or pause the current thread execution for the specified amount of time.

 

We can suspend the thread execution either by passing the time in milliseconds or with TimeSpan property like as shown below.

 

' Time in milliseconds

Thread.Sleep(1000);

Or

' Time in hours, minutes, seconds

Dim ts As TimeSpan = New TimeSpan(0, 0, 1)

Thread.Sleep(ts);

If you observe the above code snippet, we trying to suspend the thread for 1 second by using the Sleep method.

Visual Basic Sleep(milliseconds) Method Example

Following is the example of thread Sleep method in visual basic to pause the thread execution for the specified amount of time by passing the time in milliseconds.

 

Imports System.Threading

 

Module Module1

    Sub Main(ByVal args As String())

        For i As Integer = 1 To 5

            Console.WriteLine("Thread paused for {0} second", 1)

            ' Pause thread for 1 sesc

            Thread.Sleep(1000)

            Console.WriteLine("i value: {0}", i)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are suspending the main thread execution (1 second) for every iteration of For loop by passing the time in milliseconds to the Sleep method.

 

When we execute the above program, we will get the result like as shown below.

 

Thread paused for 1 second

i value: 1

Thread paused for 1 second

i value: 2

Thread paused for 1 second

i value: 3

Thread paused for 1 second

i value: 4

Thread paused for 1 second

i value: 5

If you observe the above result, for every iteration the Sleep method has suspended the thread for 1 second before it prints the value.

Visual Basic Sleep(TimeSpan) Method Example

Following is the example of Sleep method in visual basic to pause the thread execution for the specified amount of time by passing the time using TimeSpan property.

 

Imports System.Threading

 

Module Module1

    Sub Main(ByVal args As String())

        Dim ts As TimeSpan = New TimeSpan(0, 0, 1)

        For i As Integer = 1 To 5

            Console.WriteLine("Thread paused for {0} second", 1)

            ' Pause thread for 1 sesc

            Thread.Sleep(ts)

            Console.WriteLine("i value: {0}", i)

        Next

        Console.WriteLine("Main Thread Exists")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are suspending the main thread execution (1 second) for every iteration of For loop by passing the time using TimeSpan property to the Sleep method.

 

When we execute the above program, we will get the result like as shown below.

 

Thread paused for 1 second

i value: 1

Thread paused for 1 second

i value: 2

Thread paused for 1 second

i value: 3

Thread paused for 1 second

i value: 4

Thread paused for 1 second

i value: 5

Main Thread Exists

If you observe the above result, for every iteration the Sleep method has suspended the thread for 1 second before it prints the value.

 

This is how we can use the Sleep method in visual basic to suspend or pause the thread execution for the specified amount of time based on our requirements.