多线程编程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 的老大们有没注意,利用他特性实现的多线程,是初始化的一个新的线程,而这个线程中初始化的对象则是整个工程。很大的资源浪费,而且有时候还不好控制,不是你想要的效果。总之一句话,新线程中要自己初始化新的对象而且最好不要用实例继承