Vb. NET Multithreading Application

Source: Internet
Author: User
Tags join sleep thread class

Developers have been asking Microsoft to add more multi-threaded functionality to VB, as well as for vb.net. VB6 has supported the creation of multithreaded EXE, DLL, and OCX. However, using multithreading is probably not a very accurate word. Therefore VB6 only supports cells that run multiple single-threaded threads. A unit is actually a space for code execution, and the bounds of the cell limit the access of the code to anything other than the unit.

Vb. NET is different, it supports the establishment of free thread (free-threaded) applications. This means that multiple threads can access the same set of shared data. The following sections of this article discuss some of the basic points of multithreading.

Problem

Although VB6 supports multiple single-threaded units, it does not support a free threading model that does not allow multiple threads to use the same set of data. In many cases, you need to create a new thread to do background processing, which can improve the usability of the application, otherwise, a long processing can make the program's response slow, for example, you press a Cancel button on the table, but for a long time did not respond.

Solutions

Because VB.net uses the CLR (Common Language Runtime), it has many new features, one of which is the application that can create free threads.

Using threads

In vb.net, it's easy to use threads. We'll cover the details later, and now we'll start by creating a simple table that uses a new thread to run a background processing. The first thing to do is to create a background task that runs on a new thread. The following code performs a fairly long run-an infinite loop:

Private Sub backgroundprocess ()

Dim I as Integer = 1

Do While True

ListBox1.Items.Add ("iterations:" + i)

i + 1

Loop

End Sub

This code loops infinitely, and adds an item to a list box on the table each time it is executed. If you are unfamiliar with vb.net, you will find that there are some differences between this code and VB6:

. In declaring variable Dim i as Integer = 1 o'clock Assignment

. Use + = operator i = 1 instead of i = i + 1

. The call keyword is not used

Once we have a job deal, we need to assign this code to a thread and start it. To do this we want to use thread object, which is. NET Schema class as part of the System.Threading namespace. When instantiating a new thread class, we will pass it to a reference to the code block that the thread class constructor executes. The following code creates a new thread object and passes a reference to backgroundprocess to it:

Dim T as Thread

t = New Thread (AddressOf me.backgroundprocess)

T.start ()

The AddressOf operator creates a delegate object to the Backgroundprocess method. In vb.net, a delegate is a type-safe, object-oriented function pointer. After instantiating the thread, you can start executing code by calling the thread's start () method.

Control threads

After the thread is started, you can control its state through a method of the threading object. You can pause the execution of a thread by calling the Thread.Sleep method, which can receive an integer value to determine when the thread sleeps. Take the previous example, if you want to increase the speed of the list item, you can put a call to the Sleep method in it:

Private Sub backgroundprocess ()

Dim I as Integer = 1

Do While True

ListBox1.Items.Add ("iterations:" + i)

i + 1

Thread.CurrentThread.Sleep (2000)

Loop

End Sub

CurrentThread is a public static property value that allows you to get a reference to the currently running thread.

You can also put a thread into hibernation by calling Thread.Sleep (System.Threading.Timeout.Infinite), which is, in particular, the dormant time of the call is indeterminate. To break this hibernation, you can call the Thread.Interrupt method.

Similar to hibernation and interrupts is suspension and recovery. Suspension allows you to suspend a thread until another thread calls Thread.Resume. The difference between hibernation and hanging is that the latter does not immediately allow the thread to enter a waiting state, and the thread does not hang until. NET runtime that it is now a safe place to suspend it, while hibernation will immediately let the thread into a waiting state.

The last thing to introduce is thread.abort, which stops the execution of a thread. In our simple example, if you want to add a button to stop processing, it's simply that we just 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 the strong point of multithreading. The user interface responds well because it runs in a separate thread, and the processing in the background runs on another thread. When the user presses the Cancel button, it responds immediately and stops processing.

The above example is just a fairly simple application. In programming, you also need to use many of the complex features of multithreading. One of the problems is how to pass data from a program to or from the constructor of a thread class, which means that you cannot pass parameters to it or return a value to a procedure that is placed in another thread. This is because the process you pass in to the thread builder cannot have any arguments or return values. To solve this problem, you can encapsulate your process into a class so that the parameters of the method can use the fields in the class.

Here we give a simple example, if we want to compute the square of a number, that is:

Function Square (ByVal Value as double) as double

return value * value

End Function

To use this procedure in a new thread, we encapsulate 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 process on a new thread, as follows:

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 starts, we don't check the square value in the class, because even if you call the thread's Start method, you can't make sure that the method is done immediately. To get a value from another thread, there are several ways in which the method used is the simplest, which is to trigger an event when the thread completes. We'll discuss another method in a later thread synchronization. The following code adds an event declaration for 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

The method of capturing an event in the calling code is similar to VB6, you still have to declare the WithEvents variable and handle the event in one procedure. Somewhat differently, you declare that the process of handling an event uses the Handles keyword rather than the object_event that is commonly used 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

For this approach, note that the process of handling the event, in this case Squareeventhandler, will run in the thread that generated the event. It is not running on a thread that executes in a table.

Synchronizing threads

The synchronization aspect of the thread, VB. NET provides several methods. In the square example above, you want to synchronize with the thread that performs the calculation, waiting for it to execute and get the result. Another example is that if you sort an array in another thread, you must wait for the processing to complete before using the array. In order to perform these synchronizations, VB. NET provides SyncLock declarations and Thread.Join methods.

SyncLock can get a unique lock for an object reference, as long as the object is routed to SyncLock. By getting this unique lock, you can ensure that multiple threads do not access shared data or code executed on multiple threads. To get a lock, you can use a more convenient object-the System.Type object associated with each class. The System.Type object can be obtained by using the GetType method:

Public Sub Calcsquare ()

SyncLock GetType (Squareclass)

Square = value * value

End SyncLock

End Sub

The other is the Thread.Join method, which allows you to wait for a specific time until a thread completes. If the thread completes before the time you specify, Thread.Join returns TRUE, otherwise it returns false. In the case of the square, if you do not want to use the method that triggers the event, you can call the Thread.Join method to determine whether the calculation is complete. The code looks like this:

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 ()

If T.join (Then)

MsgBox (Osquare.square)

End If

End Sub

For this approach, note that the process of handling the event, in this case Squareeventhandler, will run in the thread that generated the event. It is not running on a thread that executes in a table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.