Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Implementing Multiple Threads using VB.Net 2.0

Author: Zunnair

This Article helps you to understand the use multiple threads to achieve concurrency in tasks. This Article does not include synchronization, because no data is being accessed by multiple threads.

This Article does not include synchronization, as it is not needed for this code. However, when using multiple threads and sharing data, synchronization is critical.

Because multi-threaded code presents complexities when stepping through it, the Debugger Step Through attribute is applied at the multithreaded points. Attempting to step through the multi-threaded sections may yield inconsistent results. This example only writes to each window from the thread that is managing it. When writing to a window from threads other than the thread managing that window, additional attention is required. An asynchronous delegate is an easy way to run a task on a worker thread, but is not the only way to do so.

Source Code is Given For Your Bettter Understanding.

lblThreadID.Text &= CStr(AppDomain.GetCurrentThreadId())


Then make this event function.and write the code.

Private Sub cmdSameThread_Click(ByVal sender As System.Object, _

                                 ByVal e As System.EventArgs) _

                                 Handles cmdSameThread.Click


'Run the task on the same thread that is managing the frmMain window.



End Sub

To run the task on a worker pool thread, you can use an asynchronous invocation on a delegate. For this example, we'll declare a delegate named TaskDelegate, and call it asynchronously. The signature of the delegate declaration must match the method (TheLongRunningTask)exactly.


<DebuggerStepThrough()> Private Sub cmdWorkerPoolThread_Click( _

                                      ByVal sender As System.Object, _

                                      ByVal e As System.EventArgs) _

                                      Handles cmdWorkerPoolThread.Click

To run the task an a thread from the worker pool, create an instance of a delegate whose signature matches the method that will be called, then call BeginInvoke on that delegate. For this example, the arguments and return value of BeginInvoke are unneeded. This technique is sometimes referred to as "Fire and Forget".

Dim td As New TaskDelegate(AddressOf TheLongRunningTask) td.BeginInvoke(Nothing, Nothing)

Runs on a worker thread from the pool

End Sub


>DebuggerStepThrough()< Private Sub cmdRunOnNewWin32Thread_Click( _

ByVal sender As System.Object, _ByVal e As System.EventArgs) _

                                         Handles cmdRunOnNewWin32Thread.Click


To run the task on a newly created OS thread (rather than a tread from the thread pool), create a new instance of a managed thread. The constructor takes the address of a subroutine with no arguments.


Dim t As New Thread(AddressOf TheLongRunningTask)

'Creates the new thread


'Begins the execution of the new thread

End Sub


<DebuggerStepThrough()> Private Sub TheLongRunningTask()

This method simulates some long-running task. To represent the work in

progress, a form with a progress bar will display during its execution.

The method displays the form, then updates the progress bar every half

second for 5 seconds. After simulating the task, the form is taken down.

Dim f As New frmTaskProgress()




Refresh causes an instant (non-posted) display of the label.

Slowly increment the progress bar


        Dim i As Integer

        For i = 1 To 10

            f.prgTaskProgress.Value += 10

            Thread.CurrentThread.Sleep(500) 'half-second delay



        'Remove the form after the "task" finishes



End Sub

Article Comments

I am working in ERP,QC module. I need sample code for saving bulk data nearly 150 * 150 with in few seconds with out breaking up the data ..... Am planning to use asynchronus threading to save this type of trasaction . If any one knows this concept plz help me on this ..


Posted on 12/2/2008 2:44:09 AM by DurgaMurali

Add Article Comment:
Name :
Email Address :
Comments :
<< Getting Active Processes Of OS using Windows32 API in Visual Basic.Net

Disclaimer - Privacy
© 2002-2017