What is VB.NET Thread?
When two or more processes execute simultaneously in a program, the process is known as multithreading. And the execution of each process is known as the thread. A single thread is used to execute a single logic or task in an application. By default, each application has one or more threads to execute each process, and that thread is known as the main thread.
To create and access a new thread in the Thread class, we need to import the System.Threading namespace. When the execution of a program begins in VB.NET, the Main thread is automatically called to handle the program logic. And if we create another thread to execute the process in Thread class, the new thread will become the child thread for the main thread.
Create a new Thread
In VB.NET, we can create a thread by extending the Thread class and pass the ThreadStart delegate as an argument to the Thread constructor. A ThreadStart() is a method executed by the new thread. We need to call the Start() method to start the execution of the new thread because it is initially in the unstart state. And the PrintInfo parameter contains an executable statement that is executed when creating a new thread.
Let's write a program to create and access the thread in Thread class.
In the above program, the main and child threads begin their execution simultaneously. The execution of the main thread is stopped after completing its function, but the child thread will continue to execute until its task is finished.
VB.NET Thread Methods
The following are the most commonly used methods of Thread class.
VB.NET Thread Life Cycle
In VB.NET Multithreading, each thread has a life cycle that starts when a new object is created using the Thread Class. Once the task defined by the thread class is complete, the life cycle of a thread will get the end.
There are some states of thread cycle in VB.NET programming.
Let's create a program to manage a thread by using various methods of Thread Class.
In the above example, we used a different method of the Thread class such as the Start() method to start execution of the thread, the Join() method is used to stop the execution of the thread until the execution of the thread was completed. The Sleep() method is used to pause the execution of threads for 5 seconds.
When two or more processes are executed in a program to simultaneously perform multiple tasks, the process is known as multithreading.
When we execute an application, the Main thread will automatically be called to execute the programming logic synchronously, which means it executes one process after another. In this way, the second process has to wait until the first process is completed, and it takes time. To overcome that situation, VB.NET introduces a new concept Multithreading to execute multiple tasks at the same time by creating multiple threads in a program.
Let's write a program of multiple threads to execute the multiple tasks at the same time in the VB.NET application.
In the above example, we have created two threads (th, th2) to execute the PrintInfo and PrintInfo2 method at the same time. And when execution starts, both threads execute simultaneously. But the first statement of the PrintInfo method is executed, and then it waits for the next statement until the PrintInfo2 method is completed in the program.