Javatpoint Logo
Javatpoint Logo

VB.NET Multithreading

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.

create_Thread.vb

Output:

VB.NET Multithreading

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.

Method Description
Abort() As the name defines, it is used to terminate the execution of a thread.
AllocateDataSlot() It is used to create a slot for unnamed data on all threads.
AllocateNamedDatsSlot() It is used to create a slot for defined data on all threads.
Equals() It is used to check whether the current and defined thread object are equal.
Interrupt() It is used to interrupt a thread from the Wait, sleep, and join thread state.
Join() It is a synchronization method that stops the calling thread until the execution thread completes.
Resume() As the name suggests, a Resume() method is used to resume a thread that has been suspended.
Sleep() It is used to suspend the currently executing thread for a specified time.
Start() It is used to start the execution of thread or change the state of an ongoing instance.
Suspend() It is used to stop the currently executing thread.

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.

State Description
Unstarted When we create a new thread, it is initially in an unstarted state.
Runnable When we call a Start() method to prepare a thread for running, the runnable situation occurs.
Running A Running state represents that the current thread is running.
Not Runnable It indicates that the thread is not in a runnable state, which means that the thread in sleep() or wait() or suspend() or is blocked by the I/O operation.
Dead If the thread is in a dead state, either the thread has been completed its work or aborted.

Let's create a program to manage a thread by using various methods of Thread Class.

Thread_cycle.vb

Output:

VB.NET Multithreading

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.

Multithreading

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.

Multi_thread.vb

Output:

VB.NET Multithreading

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA