VB.Net program to create a simple thread

Here, we are going to learn how to create a simple thread in VB.Net?
Submitted by Nidhi, on January 04, 2021 [Last updated : March 08, 2023]

Create a simple thread in VB.Net

Here, we will use the Thread class and import the System.Threading namespace, and create a thread function and bind the thread function with a thread object, and start the newly created thread.

Program/Source Code:

The source code to create a simple thread is given below. The given program is compiled and executed successfully.

VB.Net code to create a simple thread

'VB.net program to create a simple thread.

Imports System.Threading

Module Module1
    Class ThreadEx
        Public Sub MyThreadFun()
            For i = 1 To 5 Step 1
                Console.WriteLine("Thread Executed")
            Next
        End Sub
    End Class

    Sub Main()
        Dim T As New ThreadEx()

        Dim t1 As New Thread(New ThreadStart(AddressOf T.MyThreadFun))

        t1.Start()
    End Sub
End Module

Output

Thread Executed
Thread Executed
Thread Executed
Thread Executed
Thread Executed
Press any key to continue . . .

Explanation

In the above program, we imported the System.Threading namespace to implement multithreading in the program. After that, we created a module Module1. The Module1 contains a class ThreadEx that contains the function. Then we created a Main() function.

The Main() function is the entry point for the program, Here, we created the object of ThreadEx class and then created the object of Thread class and then bind the function MyThreadFun() to the thread object and start the thread, which will print some messages on the console screen.

VB.Net Threading Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.