這個沒法做到。原因
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、呂梁ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的呂梁網(wǎng)站制作公司
1)當(dāng)你將方法排入線程池隊列后,此方法在有線程池線程變得可用時執(zhí)行。
2)線程池中某個線程的可用與不可用,是由.net 后臺決定,用戶程序無法控制的
3)正如你觀察到的:線程啟動的時間不同,有快有慢,這恰恰說明線程池起作用了:線程池的調(diào)度試圖讓程序響應(yīng)達(dá)到最佳。
Sub Main() Dim thr As New Thread(AddressOf 循環(huán)) thr.Start("a") End Sub Sub 循環(huán)(a() As String) '這里隨你干什么循環(huán)也行 For Each i As String In a MsgBox(i) Next End Sub
多線程一般是不推薦用的,因為線程之間如果有共享資源的話會引起競爭,需要加鎖處理;而且線程間沒有時序關(guān)系,所以你在調(diào)試中可能會出現(xiàn)異步處理結(jié)束順序與開始處理順序不一致的情況(我在調(diào)試中已經(jīng)發(fā)現(xiàn)該問題)。
針對你提出的這個問題,采用了多線程處理,利用的是BackgroundWorker也就是異步處理控件進行了處理。
代碼已經(jīng)經(jīng)過調(diào)試通過。歡迎交流,如有問題,留下QQ或其他聯(lián)系方式。
代碼如下,并附程序截圖。
‘---------------------------------------------------
Imports?System.ComponentModel?'導(dǎo)入異步控件命名空間
Public?Class?Form1
Private?howmany?As?Integer?=?10
Private?AnalysisNumber(0?To?howmany?-?1)?As?BackgroundWorker
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
ListBox1.Items.Clear()
creatNewBackgroundWorker()
addHandle()
startWork()
End?Sub
Private?Sub?creatNewBackgroundWorker()
For?i?As?Integer?=?0?To?AnalysisNumber.Length?-?1
AnalysisNumber(i)?=?New?BackgroundWorker
Next
End?Sub
Private?Sub?addHandle()
For?i?As?Integer?=?0?To?AnalysisNumber.Length?-?1
AddHandler?AnalysisNumber(i).DoWork,?AddressOf?AnalysisNumber_DoWork
AddHandler?AnalysisNumber(i).RunWorkerCompleted,?AddressOf?AnalysisNumber_RunWorkerCompleted
Next
End?Sub
Private?Sub?startWork()
For?i?As?Integer?=?0?To?9
Dim?temp(0?To?9)?As?Integer
For?j?As?Integer?=?1?To?10
temp(j?-?1)?=?10?*?i?+?j
Next
AnalysisNumber(i).RunWorkerAsync(temp)
Next
End?Sub
Private?Sub?AnalysisNumber_DoWork(ByVal?sender?As?Object,?ByVal?e?As?System.ComponentModel.DoWorkEventArgs)
Dim?data?As?Integer()
data?=?CType(e.Argument,?Integer())
Dim?temp?As?Integer
For?i?As?Integer?=?0?To?data.Length?-?1
temp?=?data(i)
data(i)?=?temp?*?temp
Next
e.Result?=?data
End?Sub
Private?Sub?AnalysisNumber_RunWorkerCompleted(ByVal?sender?As?Object,?ByVal?e?As?System.ComponentModel.RunWorkerCompletedEventArgs)
Dim?data?As?Integer()
data?=?CType(e.Result,?Integer())
For?i?As?Integer?=?0?To?data.Length?-?1
ListBox1.Items.Add(data(i))
Next
End?Sub
End?Class
vb6可以實現(xiàn)多線程,不過比較麻煩,vb.net完全支持多線程,請參見msdn.
至于vb6實現(xiàn)多線程請看CreateThread這個API函數(shù).以下有詳細(xì)方案:
用VB寫多線程程序用到的第一個API函數(shù)是CreateThread,這個函數(shù)的聲明如下:
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long)As Long
這個函數(shù)的返回值是線程的句柄,它的參數(shù)含義如下:
1.lpThreadAttributes:這個參數(shù)表明函數(shù)的返回句柄是否可被子進程繼承,如果可被繼承,則指向一個SECURITY_ATTRIBUTES的結(jié)構(gòu),否則設(shè)為vbnull。
2.dwStackSize:這個參數(shù)設(shè)置線程的堆棧大小。
3.lpStartAddress:這個參數(shù)指明這個線程函數(shù)的起始地址。
4.lpParameter:這個參數(shù)是傳給線程函數(shù)的參數(shù)。
5.dwCreationFlags:這個參數(shù)設(shè)置當(dāng)線程創(chuàng)建時的初始狀態(tài),掛起,運行等等。
6.lpThreadId:這個參數(shù)是待創(chuàng)建線程的ID號。
假設(shè)創(chuàng)建一個管理線程的類clsThreads,用類的一個公用函數(shù)Initialize來初始化線程,用ResumeThread和SuspendThread函數(shù)來激活線程,實現(xiàn)代碼如下:
Private Type udtThread
Handle As Long
Enabled As Boolean
End Type
Private uThread As udtThread
Private Const CREATE_SUSPENDED As Long = H4
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Public Sub Initialize(ByVal lpfnBasFunc As Long) '初始化線程
Dim lStackSize As Long, lCreationFlags As Long, lpThreadId As Long, lNull As Long
On Error Resume Next
lNull = 0 '創(chuàng)建一個空指針
lStackSize = 0 '0表示用exe的stack size
lCreationFlags = CREATE_SUSPENDED '表示初始化后先不激活,讓別人來激活
uThread.Handle = CreateThread(lNull, lStackSize, lpfnBasFunc, lNull, lCreationFlags, lpThreadId)
If uThread.Handle = lNull Then MsgBox "Create thread failed!"
End Sub
Public Property Get Enabled() As Boolean
On Error Resume Next
Enabled = uThread.Enabled
End Property
Public Property Let Enabled(ByVal vNewValue As Boolean)
On Error Resume Next
If vNewValue And (Not uThread.Enabled) Then
ResumeThread uThread.Handle '激活線程
uThread.Enabled = True
ElseIf uThread.Enabled Then
SuspendThread uThread.Handle
uThread.Enabled = False
End If
End Property
Private Sub Class_Terminate() '終止線程
On Error Resume Next
Call TerminateThread(uThread.Handle, 0)
End Sub
以上內(nèi)容在類模塊clsThreads中,有了它就可以在窗體中實現(xiàn)多線程了。下面以一個簡單的例子來說明如何創(chuàng)建多線程(以2個線程為例)。
在form1中添加picture1和picture2兩個圖片框,以及一個command1按鈕,在工程中添加一個bas模塊,該模塊的代碼如下:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Public Sub FlickerTop()
Static BgColor As Long
Dim lTick As Long, lCounter As Long
On Error Resume Next
For lCounter = 0 To 60000
BgColor = lCounter Mod 256
Form1.Picture1.BackColor = RGB(BgColor, 0, 0) '變化圖片框的顏色
lTick = GetTickCount
While GetTickCount - lTick 10 '延遲10個毫秒時間
Wend
Next
End Sub
Public Sub FlickerBottom()
Static BgColor As Long
Dim lTick As Long, lCounter As Long
On Error Resume Next
For lCounter = 0 To 60000
BgColor = lCounter Mod 256
Form1.Picture2.BackColor = RGB(0, BgColor, 0)
lTick = GetTickCount
While GetTickCount - lTick 10
Wend
Next
End Sub
最后在form1中用下面的代碼來創(chuàng)建兩個線程,
Option Explicit
Public myThreadTop As New clsThreads, myThreadBottom As New clsThreads
Private Sub Command1_Click()
On Error Resume Next
With myThreadTop
.Initialize AddressOf FlickerTop
.Enabled = True
End With
With myThreadBottom
.Initialize AddressOf FlickerBottom
.Enabled = True
End With
MsgBox "看看會有什么..."
Set myThreadTop = Nothing
Set myThreadBottom = Nothing
End Sub
Sub Main()
Dim thr As Thread
For Pi As Integer=0 To 4 //啟用5線程
MulParams =Pi vbTab sFile vbTab dFile vbTab 1 vbTab DelN vbTab cr vbTab cg vbTab cb vbTab IndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //啟動多線程進程
Application.DoEvents
Next
End Sub