Home »
VB.Net »
VB.Net Programs
VB.Net program to create a thread pool
Here, we are going to learn how to create a thread pool in VB.Net?
Submitted by Nidhi, on January 04, 2021 [Last updated : March 08, 2023]
Create a thread pool in VB.Net
Here, we will use Thread class and import the System.Threading namespace, and create a class that contains two functions and bind the functions using ThreadPool class.
Program/Source Code:
The source code to create a thread pool is given below. The given program is compiled and executed successfully.
VB.Net code to create a thread pool
'VB.net program to create a thread pool.
Imports System.Threading
Module Module1
Class ThreadEx
Public Sub MyThreadFun1()
For i = 1 To 5 Step 1
Console.WriteLine("Thread1 Executed")
Next
End Sub
Public Sub MyThreadFun2()
For i = 1 To 5 Step 1
Console.WriteLine("Thread2 Executed")
Next
End Sub
End Class
Sub Main()
Dim T As New ThreadEx()
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T.MyThreadFun1))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T.MyThreadFun2))
End Sub
End Module
Output
Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread1 Executed
Thread2 Executed
Thread2 Executed
Thread2 Executed
Thread2 Executed
Thread2 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. Module1 contains a class ThreadEx that contains two functions. Then we created a Main() function.
The Main() function is the entry point for the program, And, we created the object of ThreadEx class and then add the functions in the thread pool using QuickeUserWorkItem() method of ThreadPool class.
VB.Net Threading Programs »