Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Serial Number in SSRS Articles
Get Started Developing for Android Apps with Eclipse Articles
How to Print a Crystal Report direct to printer Articles
Razor View Engine Interview Questions Answers Interview Questions
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
SPIRE PDF Library Articles
Infosys Interview Questions Interview Questions
Html5 interview questions and answers Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
SharePoint 2010 interview Questions Answers Interview Questions
Submit Articles | More Articles..

What is thread and how to Use it.

Posted By: nehasinhar On:5/1/2011 11:26:35 PM in:Articles Category:OOPS Hits:2221
When we want to run one or more instances of a method, we make use of threading. Suppose we have a method like this...

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 once you call the start method of the thread. There are a few ways to retrieve values back from another thread. The easiest way is to raise an event when the thread is complete. We will examine another method in the next section on thread synchronization. The following code adds the event declarations to the SquareClass.

Public Class SquareClass
 Public Value As Double
 Public Square As Double
 Public Event ThreadComplete(ByVal Square As Double)
 Public Sub CalcSquare()
  Square = Value * Value
  RaiseEvent ThreadComplete(Square)
  End Sub
End Class 

Catching the events in the calling code has not changed much from VB6, you still declare the variables WithEvents and handle the event in a procedure. The part that has changed is that you declare that a procedure handles the event using the Handles keyword and not through the naming convention of Object_Event as in VB6. Dim WithEvents oSquare As SquareClass

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


Sub SquareEventHandler(ByVal Square As Double) _
  Handles oSquare.ThreadComplete
  MsgBox("The square is " & Square)
End Sub

The one thing to note with this method is that the procedure handling the event, in this case SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from which the form is executing.
comments powered by Disqus
User Profile
neha sinha
Software Engineer
NA , NA
Email :You must Log In to access the contact details.
Latest Post from :nehasinhar
What is thread and how to Use it.
View: 2221 | Submitted on: 5/1/2011 11:26:35 PM
Submit Articles | All Post of This User..


Advertise About Us Private Policy Terms of use
All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher