Vb. NET Multithreaded Development instance

Source: Internet
Author: User
Tags garbage collection integer thread
Multithreading VB. NET (Visual Basic.NET) is to accommodate Microsoft. NET Framework, the development tools for Visual Basic have been significantly modified. It is more powerful and easier to use than the Visual Basic 6.0 feature. One of the most important changes is object inheritance, in which all manageable types are derived from System.Object vb.net. As a programming tool, one of the most important features is garbage collection, which is controlled by the CLR (Common Language Runtime) and provides better memory management capabilities. generic type definitions can provide better interoperability and collaboration capabilities, so VB. NET appears stronger and more reliable.
In vb.net, most CLR-built types are already defined in the System namespace. For example: System.Object, System.Int32, and System.String. Note that a namespace may be embedded within another namespace, like a System.Data.DataSet class in System.Data.
Represents a new concept that is important to the CLR programming model. A representative is a special type of manageable class, and when you create a representative instance, you must provide an address with a method executed with a matching signature, and once a representative instance is created, invoking the method becomes easy.
In the past, we use VB to develop multi-threaded applications, is a very painful thing, is often multithreaded program running is going to become a lot of wrong program! But in the vb.net, the situation has changed dramatically. Now, we use vb.net to handle multithreading as simple as using Java to handle multithreading. Below we give an example, to see vb.net multithreading bar!
The following is the code for multithreaded Threadtest.vb:
 
Imports System
Imports System.Threading
 
public class AClass
 
Public Sub Method1 ()
Dim I As Integer
For i = 1 to 100
Console.WriteLine ("This is the content of the class AClass method Method1", i)
Next
End Sub
 
Public Sub Method2 ()
Dim I As Integer
For i = 1 to 100
Console.WriteLine ("This is the content of the class AClass method Method2", i)
Next
End Sub
 
End Class
 
public class ThreadTest
 
Public Shared Sub Main ()
Dim obj As New AClass
Dim th1,th2 As Thread
 
Th1=new Thread (New ThreadStart (AddressOf Obj.method1))
Th1.start
 
Th2=new Thread (New ThreadStart (AddressOf Obj.method2))
Th2.start
 
Dim i As Integer
For i= 1 to 100
Console.WriteLine ("Content in Main method", I)
Next
End Sub
 
End Class
 
Now, let's dissect the example above:
1. We created our own class AClass and created two methods: Method1 and METHOD2.
2. The two methods are simple, with only a for loop that outputs some information to the output device.
3. We also define another class threadtest to use the class AClass created above.
4. In the main () method, we created an instance of class thread.
5. Class thread can be obtained in the System.Threading namespace, which defines the properties and methods of the processing thread.
6. In the constructor for class thread, we use Class ThreadStart, a class ThreadStart is a representation that marks the start of a defined method when a thread starts.
7. In order to execute the defined method, we actually call the thread's start () method.
8 use VBC to compile the above program:
Vbc/out:threadtest.exe Threadtest.vb
9. After running the compiled program, we will see the mixed output of the two methods we defined and the main () method, which means that each method runs under its own thread.
10. In addition to the above methods, threads also have the following common methods:
Stop (): Stops the thread from running.
Suspend (): Suspends running of threads.
Resume (): Continue the running of the thread.
Sleep (): Stop a thread for a period of time (in milliseconds).
 
The above is just a simple example of vb.net multithreading, I hope to inspire you!


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.