VB. NET multi-thread Development Instance

Source: Internet
Author: User

VB. NET (Visual Basic. NET) is a development tool after major transformation of Visual Basic to meet the needs of the Microsoft. NET Framework. It is more powerful and easy to use than Visual Basic 6.0. The most important change is Object Inheritance. In VB. NET, All manageable types are derived from System. Object. As a programming tool, the most important feature is garbage collection, which is controlled by CLR (Common Language Runtime) and provides better memory management functions. Common Type Definitions can provide better interoperability and collaboration capabilities. Therefore, VB. NET is more powerful and reliable.
In VB. NET, Most built-in CLR types are defined in the System namespace. For example, System. Object, System. Int32, and System. String. Note that one namespace may be embedded in another namespace, such as the System. Data. DataSet class in System. Data.
Representative is a new concept that is very important to the CLR programming model. Represents a special type of manageable class. When you create an instance, you must provide an address for executing a method with a matching signature, once an instance is created, it is easy to call the method.
In the past, when we used VB to develop multi-threaded applications, it was a very painful thing. It was often because a multi-threaded program would become a multi-error program! However, in VB. NET, this situation has been greatly improved. Now, using VB. NET to process multithreading is as simple as using JAVA to process multithreading. Let's take an example. Let's take a look at the multithreading of VB. NET!
The code for threadtest. vb is as follows:

Imports System
Imports System. Threading

Public class AClass

Public sub Method1 ()
Dim I as integer
For 1 to 100
Console. Writeline ("this is the content of the AClass method method1", I)
Next
End sub

Public sub Method2 ()
Dim I as integer
For 1 to 100
Console. Writeline ("this is the content of the AClass method method2", I)
Next
End sub

End class

Public class ThreadTest

Public shared sub Main ()
Dim obj as new aclass
Dim th1, th1 as thread

Th1 = new Thread (new ThreadStart (addressof obj. method1 ))
Th1.start

2nd = new Thread (new ThreadStart (addressof obj. method2 ))
Th2.start

Dim I as integer
For 1 to 100
Console. WriteLine ("Main method content", I)
Next
End sub

End class

Now let's take a look at the above example:
1. We created our own class AClass and created two methods: Method1 and Method2.
2. The two methods are simple. There is only one For loop, which 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 a Thread-like instance.
5. The class Thread can be obtained in the System. Threading namespace. It defines the attributes and methods for processing threads.
6. In the constructor of the class Thread, we use the class ThreadStart, which is a representative of the class ThreadStart, marking the method for executing the definition when a Thread starts.
7. To execute the defined method, we actually call the Start () method of the thread.
8. Use VBC to compile the above program:
Vbc/out: threadtest.exe threadtest. vb
9. Run the compiled program. We will see the mixed output of the two methods we have defined and the main () method. This shows that each method runs in its own thread.
10. In addition to the above method, the thread also has the following common methods:
Stop (): Stop the running of the thread.
Suspend (): Pause the running of the thread.
Resume (): continues the running of the thread.
Sleep (): stops a thread for a period of time (unit: milliseconds ).
 
The above is just a simple example of VB. NET multithreading. I hope you will be inspired!

Related Article

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.