What is a thread? How to use and create a thread in .NET?

Q

What is a thread? How to use and create a thread in .NET? Can we use events with threading?

✍: Guest

A

Threads - When we want to run one or more instances of a method, we make use of threading. Suppose we have a method like this...

Private Sub OnGoingProcess()
  Dim i As Integer = 1
  Do While True
   ListBox1.Items.Add("Repeatitions: " + i)
   i += 1
  Loop
End Sub

Dim t As Thread
t = New Thread(AddressOf Me.OnGoingProcess)
t.Start()

The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread. After the thread is started, you have some control over the state of it by using methods of the Thread object. You can pause a thread's execution by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. If you wanted to slow down the addition of items to the listbox in the example above, place a call to the sleep method in this code:

Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
  ListBox1.Items.Add("Repeatitions: " + i)
  i += 1
  Thread.CurrentThread.Sleep(2000)
Loop
End Sub

You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method. Similar to Sleep and Interrupt are Suspend and Resume. Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state. Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another button on the form that allows us to stop the process. To do this all we would have to do is call the Thread.Abort method as follows:

Private Sub Button2_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button2.Click
 t.Abort()
End Sub 

This is where the power of multithreading can be seen. The UI seems responsive to the user because it is running in one thread and the background process is running in another thread. The cancel button immediately responds to the user's click event and processing stops. The next example shows a rather simple situation. Multithreading has many complications that we have to work out when we program. One issue that we will run into is passing data to and from the procedure passed to the constructor of the Thread class. That is to say, the procedure we want to kick off on another thread cannot be passed any parameters and we cannot return data from that procedure. This is because the procedure wepass to the thread constructor cannot have any parameters or return value. To get around this, wrap our procedure in a class where the parameters to the method are written as fields of the class. A simple example of this would be if we had a procedure that calculated the square of a number:

Function Square(ByVal Value As Double) As Double Return Value * Value End Function To make this procedure available to be used in a new thread we would wrap it in a class: Public Class SquareClass Public Value As Double Public Square As Double Public Sub CalcSquare() Square = Value * Value End Sub End Class

Use this code to start the CalcSquare procedure on a new thread. following code:

Private Sub Button1_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button1.Click
 Dim oSquare As New SquareClass()
 t = New Thread(AddressOf oSquare.CalcSquare)
 oSquare.Value = 30
 t.Start()
End Sub

Notice that after the thread is started, we do not inspect the square value of the class, because it is not guaranteed to have executed on

2009-03-06, 5409👍, 0💬