Visual Basic Multithreading

In visual basic, multithreading means executing the multiple threads simultaneously to perform multiple tasks at a time.

 

The perfect example of multithreading is the operating system. For example, in windows operating system we can open multiple applications like excel, word, notepad, and browser at a time and perform multiple tasks simultaneously. In an operating system, each application process will be taken care by separate threads.

 

As we discussed in threading in vb, by default every program will carry one thread to execute the application logic and that thread is called a Main thread. So, we can say that every program or application is by default a single-threaded model.

 

When we start a program execution, the Main thread will create automatically and it is responsible for executing the programming logic in a synchronous way that means one after another. So, the second process has to wait until the first process completes its execution, and it’s a time taking process.

 

To overcome this problem, multithreading has been introduced to execute the multiple tasks simultaneously (asynchronous way) by creating multiple threads in our application. The threads which we will create in our application will become a child threads for the Main thread.

 

In visual basic, the child threads are responsible for executing the tasks in an asynchronous way that means executing the multiple tasks simultaneously at a time.

 

In visual basic, we can create or access the threads using Thread class for that we need to import System.Threading namespace in our program.

 

To learn more about threads in visual basic, check threading in vb.

Visual Basic Single Threaded Model Example

Following is the example of creating the application without having any child threads in visual basic.

 

Imports System.Threading

 

Module Module1

    Sub Main(ByVal args As String())

        PrintInfo1()

        PrintInfo2()

        Console.ReadLine()

    End Sub

    Private Sub PrintInfo1()

        For i As Integer = 1 To 4

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

            Thread.Sleep(1000)

        Next

        Console.WriteLine("First method completed")

    End Sub

    Private Sub PrintInfo2()

        For i As Integer = 1 To 4

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

        Next

        Console.WriteLine("Second method completed")

    End Sub

End Module

If you observe the above example, we created two methods (PrintInfo1, PrintInfo2). Here, in PrintInfo1 method, we are making the Main thread to sleep for 1 second for every iteration and after 1 second it will resume its process and continue next iteration. So, the second method PrintInfo2 has wait (4 seconds) until the first method PrintInfo1 completes its execution.

 

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

 

i value: 1

i value: 2

i value: 3

i value: 4

First method completed

i value: 1

i value: 2

i value: 3

i value: 4

Second method completed

If you observe the above result, the second method (PrintInfo2) has waited until the first method (PrintInfo1) completes its execution.

 

The second method (PrintInfo2) has waited around 4 seconds to start its execution. So, to overcome the drawback of single-threaded model, we need to implement multithreading approach to execute the tasks simultaneously.

Visual Basic Multithreading Example

Following is the example of creating multiple threads to execute the multiple tasks simultaneously in visual basic.

 

Imports System.Threading

 

Module Module1

    Sub Main(ByVal args As String())

        Dim t1 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo1))

        Dim t2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo2))

        t1.Start()

        t2.Start()

        Console.ReadLine()

    End Sub

    Private Sub PrintInfo1()

        For i As Integer = 1 To 4

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

            Thread.Sleep(1000)

        Next

        Console.WriteLine("First method completed")

    End Sub

    Private Sub PrintInfo2()

        For i As Integer = 1 To 4

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

        Next

        Console.WriteLine("Second method completed")

    End Sub

End Module

If you observe the above example, we created two threads (t1, t2) using Thread class to execute PrintInfo1 & PrintInfo2 methods simultaneously. When we start the program execution, both threads (t1, t2) will start simultaneously to perform assigned tasks.

 

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

 

i value: 1

i value: 1

i value: 2

i value: 3

i value: 4

Second method completed

i value: 2

i value: 3

i value: 4

First method completed

If you observe the above result, while the first method (PrintInfo1) is waiting for next iteration, the second method (PrintInfo2) has completed its execution instead of waiting for first method (PrintInfo1) execution.

 

This is how we can implement the multithreading in visual basic to execute the multiple tasks simultaneously based on our requirements.

Topics Covered