Thursday, October 12, 2006

A simple multithreading .NET application example

Threads are also known as lightweight processes.  However, if we go into depth then we would know that a thread is not actually a process; rather it provides ways for executing different parts of a program.  Now let us discuss what it actually means by multithreading.  Multithreading (as the name suggests multi+threading) is nothing but an efficient execution of multiple threads at a time to enhance the performance of the application.  For example, we are doing a file copy operation with a status bar on UI indicating the completion percentage.  Here we need to keep track of how much file size is copied and at the same time we also need to advance the progress bar accordingly.  This can not be done efficiently in a single thread and you have to use multiple threads.

Multithreading

Whenever an application runs, it runs under a main thread.  However, running a single thread can sometimes lead to unnecessary performance and locking issues.  So if the application can be broken into multiple threads without hampering the flow of the main thread, then using it is always better.

Figure 1

a simple multithreading .net application example

The following care needs to be taken while going for multithreading.

While using the threads we need to be very careful about the flow of each individual thread. Otherwise, there is a heavy probability of deadlock creation when multithreading comes into picture.

All the sub-internal threads need to be ended before the main thread ends.

Benefits of Multithreaded Applications

The image given below shows the difference between the single threaded and multi-threaded applications.  In a single threaded application, the flow gets confined to one thread only.  Therefore, one independent portion of the application may also have to wait for a long time to get executed, resulting in an overall increase of Response and execution time.  In a multithreading environment the independent section of an application gets executed by the separate threads and can continue execution by also doing time-span overlaps.  So at any particular instant there is every possibility of execution of more than one thread, resulting in a significant rise in performance.

Figure 2

a simple multithreading .net application example

In .NET, the threading is handled through the System.Threading namespace.

Creating a variable of the System.Threading.Thread type allows you to create a new thread to start working with.

It is clear to everybody that the concept of threading is to go off and do another task.  The Thread constructor requires the address of the procedure that will do the work for the thread.

The AddressOf is the parameter that the constructor needs to begin using the thread.

Below is an example of a simple threading application.

Imports System.Threading
Public Class Form1
  Inherits System.Windows.Forms.Form
 
  Dim th1 As Thread
  Dim th2 As Thread
 
  Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    th1 = New Thread(AddressOf proc1)
    th2 = New Thread(AddressOf proc2)
 
    th1.Start()
    th2.Start()
  End Sub
 
  Sub proc1()
    Dim iCount As Integer
    For iCount = 1 To 10
      cmb1.Items.Add(iCount)
    Next
  End Sub
  Sub proc2()
    Dim iCount As Integer
    For iCount = 11 To 20
      cmb2.Items.Add(iCount)
    Next
  End Sub
End Class

Get more information

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

Comments on "A simple multithreading .NET application example"