Implement truly practical multithreading with visual Basic 6

Source: Internet
Author: User
Tags implement sleep thread
visual| Multithreading

In fact, using the ActiveX EXE Technology of Visual Basic can easily achieve multithreading.

The principle is not to say first, to give an example, if interested, we can discuss together

1. A new project, type selection ActiveX Exe, project renamed to Testexe
Add a form to the project and put a timer control on it.
Rename Class1 to Clstest, note that its instancing to be set to 5-multiuse, the following is its code:

Option Explicit
Private Declare Sub sleep Lib "kernel32" (ByVal dwmilliseconds as Long)
Private WithEvents Otimer as Timer
Private Frmtmp as Form1
Private Ltotalloop as Long
Private Bdostop as Boolean
Private Linterval as Long

Public Event Progress (ByVal lprogress as Long)


Public Sub startsub (ByVal ltotal as Long)
Ltotalloop = Ltotal
otimer.enabled = True
End Sub

Public Sub stopsub ()
Bdostop = True
End Sub

Private Sub Class_Initialize ()
Set frmtmp = New Form1
Load frmtmp
Set Otimer = Frmtmp.timer1
otimer.enabled = False
Bdostop = False
Linterval = 1
End Sub

Private Sub Doloop ()
Dim I as Long
For i = 0 to Ltotalloop
Sleep (Linterval)
RaiseEvent Progress (i)

If bdostop = True Then
Exit Sub
End If

Next
End Sub

Private Sub Class_Terminate ()
Unload frmtmp
Set frmtmp = Nothing
Set Otimer = Nothing
End Sub

Private Sub Otimer_timer ()
otimer.enabled = False
Doloop
End Sub

Public Property Get Lmillisecs () as Long
Lmillisecs = Linterval
End Property

Public Property Let Lmillisecs (ByVal Vnewvalue as Long)
Linterval = Vnewvalue
End Property

In the threading model of the Testexe property (engineering properties), set thread per Object, or select a value greater than 1 in the thread pool. If you set the thread pool to 2, call this ActiveX EXE can have up to two threads at the same time, and more requests will be placed in the queue.
Compiling Testexe

Here's how to test our multithreaded routines:

Create a new Standard EXE project, in reference Select Just do the TestExe.exe
Add two listbox in Form1, two commandbutton,command1 to start, Command2 to stop
Here's the code for PROJECT1.FORM1:

Option Explicit
Private WithEvents OTest1 as Testexe.clstest
Private WithEvents OTest2 as Testexe.clstest

Private Sub Command1_Click ()
Set OTest1 = New testexe.clstest
Otest1.lmillisecs = 100
Otest1.startsub (1000)

Set oTest2 = New testexe.clstest
Otest2.lmillisecs = 100
Otest2.startsub (1000)
End Sub

Private Sub Command2_Click ()
Otest1.stopsub
Otest2.stopsub
End Sub

Private Sub form_unload (Cancel as Integer)
Set OTest1 = Nothing
Set OTest2 = Nothing
End Sub

Private Sub otest1_progress (ByVal lprogress as Long)
List1.AddItem lprogress
List1.listindex = list1.listcount-1
End Sub

Private Sub otest2_progress (ByVal lprogress as Long)
List2.additem lprogress
List2.listindex = list2.listcount-1
End Sub

Start Project1, click Command1, how do you see the effect? Try to change Testexe's thread pool to 1 and see what happens? This is what I think is the most simple and stable multithreaded implementation method, we have any good ideas welcome to the message.



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.