Vb. Multi-threaded development in net

Source: Internet
Author: User
Tags contains implement integer thread thread class tostring
Multithreading

Introduction

For developers using VB6, to implement multithreaded (Multi-thread) functionality in a program, it is generally called using the Win32 API. However, any developer who has done this will feel the implementation is very difficult, and there will always be some null terminated strings GPF errors. But with the vb.net, all troubles become the past.

Free Thread (threaded)

In VB6, we can only set multithreaded mode on the component, which is usually the multithreading of the unit mode. For a unit thread component, each executable method in the component will run on a thread that is associated with the component. In this way, we can create a new component instance so that each instance has a corresponding thread unit, so that each running component has its own thread, and multiple components can run the method simultaneously in their own units.

But if we don't use the unit threading component within the program's session or application control, the best and only option is the free threading component. But just have VB6 not yet, we need VC + + or Java Help.

Well now, VB. NET improves these features,. Net The System.Threading namespace contained in the SDK is designed to implement multi-threaded functionality and is fairly simple: you have the properties and methods to implement free threading simply by taking advantage of the thread class in the System.Threading name space.
 

An example of creating a free thread parsing

Let's take a look at the following vb.net code, which demonstrates the process of implementing a free thread:

 

Imports System
' Import the threading namespace
Imports System.Threading

Public Class clsmultithreading
' This is the simple method ', runs a loop 5 times
' This was the ' one called by the HELLOWORLDTHREADINGINVB
' Class, on a separate thread.
Public Sub Ownthread ()
Dim intcounter As Integer
For intcounter = 1 to 5
Console.WriteLine ("Inside The Class:" & Intcounter.tostring ())
Next
End Sub
End Class

Public Class HELLOWORLDTHREADINGINVB
' This is executed ' to run
Public Shared Sub Main () Dim intcounter As Integer
' Declare An instance of the class Clsmultithreading object
Dim Objmt as Clsmultithreading = new clsmultithreading ()


' Declare An instance of the Thread object. This class
' resides in the System.Threading namespace
Dim Objnewthread as Thread


' Create a New thread with the thread Class and pass
' The address of Ownthread method of Clsmultithreading class
Objnewthread = new Thread (new ThreadStart (AddressOf objmt.ownthread))


' Start a new Thread. This basically calls the Ownthread
' Method within the Clsmultithreading class
' It is important to know so this is called on another
' Thread, as you'll be in the output
Objnewthread.start ()


' Run a loop and display count
For intcounter = 15
Console.WriteLine ("Inside The Sub Main:" & Intcounter.tostring ())
Next
End Sub
End Class

The beginning of the code is to introduce namespaces system and system.threading, and then create a common class clsmultithreading. The class clsmultithreading contains a common method ownthread that performs 5 for loops, displaying information to the screen at a time. This class is created to invoke the Ownthread method from other classes in a separate thread.

Next, we create another class HELLOWORLDTHREADINGINVB, which contains a method main. When the resulting executable file runs, main starts. In method Main, we create an instance of the Clsmultithreading class so that it can call its method ownthread.

Now we create the thread class instance by passing the address of the method Ownthread, which actually equals creating a new thread and telling the thread the method to execute when it runs. We use the function AddressOf to get the address of the method Ownthread and then pass it to the ThreadStart representative class.

We then call the thread class's Start method to start the new class. The new class starts in its own way and no longer needs to rely on the class that created it.

  Compiler

We save the above code as a file Thread.vb and compile it:

  

Vbc.exe thread.vb/t:exe/debug/cls


When the compilation succeeds, the executable file Thread.Exe is created. Running Thread.Exe, we'll get the following results:

 


Note that the output information from the Clsmultithreading class and the Hellworldthreadinginvb class is not synchronized. This means that when the Objnewthread.start () command is executed, a new thread is started, and the Ownthread method in that new thread is executed. 2 threads run side by side, and the phenomenon is that the information numbers 1 through 5 are not together, interspersed with output information from the main thread.

  Conclusion

The above is the process of creating multithreaded applications in vb.net. You can feel how simple this is, but it also implements a very powerful function. With multithreaded functionality, we can create better business and data-tier components and, with our imagination, make it better.



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.