多線程編程VB
Ⅰ VB中如何實現多線程
用CreateThread API函數根本無法實現VB6多線程!雖然系統提供了這個API函數,但是這種實現方法對VB6程序極其不穩定,容易出現非法操作。如果確實需要多線程的話,請使用控制項,此處可以下載MThreadVB
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=26900&lngWId=1
Ⅱ VB Winsock 多線程編程!
給你寫了這個代碼,一個遠程式控制制就基本寫好了(嘿嘿!!)
提示:其實用數組就好了.
Winsock(0),像這樣
Load Winsock(數目) '載入winsock
Winsock(index).Accept RequestID ' index為指定的winsock,先判斷winsock是否為sckclosed
...
Ⅲ VB多線程示例!
窗體代碼:Private Sub Command1_Click()
'為addnumber過程創建線程
Call CreateNewThread(AddressOf addnumber)
End Sub
Private Sub Command2_Click()
TerminateTh '終止線程
Unload Me '結束程序
End Sub
模塊: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 TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread 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 GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As LongPrivate Const MAXLONG = &H7FFFFFFF
Private Const THREAD_BASE_PRIORITY_IDLE = -15
Private Const THREAD_BASE_PRIORITY_LOWRT = 15
Private Const THREAD_BASE_PRIORITY_MAX = 2
Private Const THREAD_BASE_PRIORITY_MIN = -2
Private Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
Private Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
Private Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
Private Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
Private Const THREAD_PRIORITY_ERROR_RETURN = (MAXLONG)
Private Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
Private Const THREAD_PRIORITY_NORMAL = 0
Private Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
Private Const CREATE_ALWAYS = 2
Private Const CREATE_NEW = 1
Private Const CREATE_NEW_CONSOLE = &H10
Private Const CREATE_NEW_PROCESS_GROUP = &H200
Private Const CREATE_NO_WINDOW = &H8000000
Private Const CREATE_PROCESS_DEBUG_EVENT = 3
Private Const CREATE_SUSPENDED = &H4
Private Const CREATE_THREAD_DEBUG_EVENT = 2Public Enum ThreadPriority
tpLowest = THREAD_PRIORITY_LOWEST
tpBelowNormal = THREAD_PRIORITY_BELOW_NORMAL
tpNormal = THREAD_PRIORITY_NORMAL
tpAboveNormal = THREAD_PRIORITY_ABOVE_NORMAL
tpHighest = THREAD_PRIORITY_HIGHEST
End EnumPrivate mThreadHandle As Long'示例
Function addnumber() As Long
Dim i As Long
For i = 0 To 100000
Form1.Label1.Caption = i
Next
End Function'創建線程
Public Function CreateNewThread(ByVal cFunction As Long) As Long
Dim mhandle As Long
Dim lpThreadID As Long
mhandle = CreateThread(ByVal 0&, ByVal 0&, cFunction, ByVal 0&, 0, lpThreadID)
If mhandle <> 0 Then mThreadHandle = mhandle
End Function'終止線程
Public Function TerminateTh() As Long
SuspendThread mThreadHandle
TerminateThread mThreadHandle, ByVal 0&
TerminateThread lpThreadID, ByVal 0&
End Function
Ⅳ 怎樣在VB中實現多線程
VB6不行,只能模擬多線程,若非要VB6就要用到其他語言開發的類庫,若VB.NET是可以製作穩定而安全的多線程的程序的。
Ⅳ VB如何實現多線程
用CreateThread API函數根本無法實現VB6多線程!雖然系統提供了這個API函數,但是這種實現方法對VB6程序極其不穩定,容易出現非法操作。如果確實需要多線程的話,請使用控制項,此處可以下載MThreadVB
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=26900&lngWId=1
打字不易,如滿意,望採納。
Ⅵ vb 怎麼實現多線程
Vb.net簡單,vb得用API實現而且相當麻煩和不穩定 。不穩定是vb6自身控制項不是線程安全的,所以多線程統統不能用。所以界面不能用控制項而得用API實現,這樣vb多線程才穩定,但是那樣可視化開發的優勢全無。
建議用Vb.net線程簡單,只要
Dim aaa As New Thread (線程函數代碼名稱)
aaa.Start 即可啟動線程了
Ⅶ 什麼是多線程技術在VB 中如何使用
vb6可以實現多線程,不過比較麻煩,vb.net完全支持多線程,請參見msdn.
至於vb6實現多線程請看CreateThread這個API函數.以下有詳細方案:
用VB寫多線程程序用到的第一個API函數是CreateThread,這個函數的聲明如下:
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
這個函數的返回值是線程的句柄,它的參數含義如下:
1.lpThreadAttributes:這個參數表明函數的返回句柄是否可被子進程繼承,如果可被繼承,則指向一個SECURITY_ATTRIBUTES的結構,否則設為vbnull。
2.dwStackSize:這個參數設置線程的堆棧大小。
3.lpStartAddress:這個參數指明這個線程函數的起始地址。
4.lpParameter:這個參數是傳給線程函數的參數。
5.dwCreationFlags:這個參數設置當線程創建時的初始狀態,掛起,運行等等。
6.lpThreadId:這個參數是待創建線程的ID號。
假設創建一個管理線程的類clsThreads,用類的一個公用函數Initialize來初始化線程,用ResumeThread和SuspendThread函數來激活線程,實現代碼如下:
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 '創建一個空指針
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
以上內容在類模塊clsThreads中,有了它就可以在窗體中實現多線程了。下面以一個簡單的例子來說明如何創建多線程(以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中用下面的代碼來創建兩個線程,
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
Ⅷ VB怎麼樣實現多線程
據說 ACTIVEX.EXE文件 叫多進程 並非多線程~
其實你不如用VB.net來創建多線程 2,3行代碼而已
用合適的工具 解決問題才是首選
Ⅸ VB中如何利用Createthread實現多線程能給個最簡單的例子嗎
Option Explicit
Public 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
Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Public id As Long
Public Function AddText()
Do
Form1.Text1.Text = "Adding to Text1 - "
doevents
Loop
End Function
Option Explicit
Private Sub Command1_Click()
id = CreateThread(ByVal 0&, ByVal 0&, AddressOf AddText, ByVal 0&, 0, id)
End Sub
Private Sub Command2_Click()
Call TerminateThread(id, ByVal 0&)
End Sub
//PS 請注意裡面所用到的控制項...
Ⅹ VB能不能實現多線程運行,
先說多線程:
方法 1 : CreateThread
不用多說,萬變不離其宗,這是 Win32 內創建線程的唯一系統方法,以下介紹的多線程創建方式雖然不直接接觸這個 Win32 APi,但系統也是通過他創建的。
方法 2 : TimerSetEvent
這個函數是 Win32 系統的一個高精度計時器函數,通過調用這個函數,系統會創建一個新的線程,讓Timer回調函數中的代碼運行在這個新線程中。
方法 3 : SHCreateThread
很多人應該沒見過這個函數,這是 shlwapi.dll 的一個導出函數,這個函數看字面也能理解是個創建多線程的函數,而且。。。 他是基於對象操作的多線程函數,也就是說他能夠穩定的運行一些用上面2種方法創建,但不能穩定運行的代碼。至少這個函數在 IDE 的環境下表現的中規中矩! 使用方法可查 API-GUIDE
方法 4 :CreateRemoteThread
創建遠程線程,經測試一點也不穩定,根本沒法說下去了,還不如直接調用 CreateThread
但,做為一種線程的創建方法,還是希望能夠熟悉一下,如果不用VB寫代碼,要寫跨進程線程,這是一個簡單易行的辦法,否則只能繼續SetWindowHook了,熟悉匯編指令的還可以遠程分配內存寫硬編碼執行
方法 5 : 簡單說下 ActiveX 方式的多線程
用 ActiveX 創建線程與其他方式創建的線程最重要的地方要主義,任何VB函數的調用最好都建立在新線程中新初始化的對象基礎上執行,不要跨對象調用,而 ActiveX 的安全線程本身也正是利用了這點,不知道研究 ActiveX 的老大們有沒注意,利用他特性實現的多線程,是初始化的一個新的線程,而這個線程中初始化的對象則是整個工程。很大的資源浪費,而且有時候還不好控制,不是你想要的效果。總之一句話,新線程中要自己初始化新的對象而且最好不要用實例繼承