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

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 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.