vb实现多线程的问题???

lgonnet 2002-09-03 09:49:52
如何实现,需要一小段例子代码就可以了
请发到lgonnet@263.net,多谢了
...全文
73 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
daviddivad 2002-09-03
  • 打赏
  • 举报
回复
给你一个多线程的类:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsThreading"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

'=======================================多线程===================================
'注意: 当你用了多线程时,你要结束你的程序时,简单的用END是不行的,而要杀掉当前进程序
'
'Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
'Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

'eg: Call TerminateProcess(GetCurrentProcess, ByVal 0&)




'API Declarations
'Creates a new thread
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
'Terminates a thread
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
'Sets the priority of a thread
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
'Returns the proirity of a thread
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
'Enables a disabled Thread
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
'Disables a thread
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
'Returns the handle of the current thread
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
'Returns the ID of the current thread
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

'Consts
Private Const MAXLONG = &H7FFFFFFF

'Thread priority consts
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

'Thread creation flags
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 = 2

'Types and Enums
Public 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 Enum

'Vars
Private mThreadHandle As Long
Private mThreadID As Long
Private mPriority As Long
Private mEnabled As Boolean
Private mCreated As Boolean

Public Function CreateNewThread(ByVal cFunction As Long, Optional ByVal cPriority As ThreadPriority = tpNormal, Optional ByVal cEnabled As Boolean = True)
'Creates a new Thread
Dim mHandle As Long
Dim CreationFlags As Long
Dim lpThreadID As Long

'Look if the thread has already been created
If mCreated = True Then Exit Function

'Look if the thread should be enabled
If cEnabled = True Then
CreationFlags = 0
Else
'Create a disabled thread, can be enabled later with the
''Enabled' property
CreationFlags = CREATE_SUSPENDED
End If

'The CreateThread Function returns the handle of the created thread;
'if the handle is 0, it failed creating the thread
mHandle = CreateThread(ByVal 0&, ByVal 0&, cFunction, ByVal 0&, CreationFlags, lpThreadID)

If mHandle = 0 Then 'Failed creating the thread
'Insert your own error handling
'Debug.Print "InitializeThread Function in clsThreading failed creating a new thread"
Else
mThreadHandle = mHandle
mThreadID = lpThreadID
mCreated = True
End If
End Function

Public Function TerminateCurrentThread()
'Terminates the current thread

'Ignore errors to prevent crashing if no thread has been created
On Error Resume Next
'Terminate the thread to prevent crashing if the app is closed
'and the thread is still running (dangerous!)
Call TerminateThread(mThreadHandle, ByVal 0&)
mCreated = False
End Function

Public Property Get ThreadHandle() As Long
'Returns the Handle of the current Thread
ThreadHandle = mThreadHandle
End Property

Public Property Get ThreadID() As Long
'Returns the ID of the current thread
ThreadID = mThreadID
End Property

Public Property Get Priority() As Long
'Returns a long value because the thread might have other priorities
'than our five in the enum

'Ignore errors to prevent crashing if no thread has been created
On Error Resume Next
Priority = GetThreadPriority(mThreadHandle)
End Property

Public Property Let Priority(ByVal tmpValue As ThreadPriority)
'Sets the Thread Priority of the actual thread
mPriority = tmpValue
Call SetThreadPriority(mThreadHandle, tmpValue)
End Property

Public Property Get Enabled() As Boolean
'Returns whether the Thread is enabled or not
Enabled = mEnabled
End Property

Public Property Let Enabled(ByVal tmpValue As Boolean)
'Enables/Disables the Thread

'Ignore errors to prevent crashing if no thread has been created
On Error Resume Next
If tmpValue = True Then
'Enable the thread
Call ResumeThread(mThreadHandle)
ElseIf tmpValue = False Then
'Disable the thread
Call SuspendThread(mThreadHandle)
End If
End Property

Private Sub Class_Terminate()
'Terminate the thread to prevent crashing if the app is closed
'and the thread is still running (dangerous!)
Call TerminateCurrentThread
End Sub
laojing1128 2002-09-03
  • 打赏
  • 举报
回复
见帮助中的coffe,thread
lihongxing2002 2002-09-03
  • 打赏
  • 举报
回复
我也要,supperman_2002@sina.com
谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了谢谢了!!!!!!!!!!!!!!!11
◆ 01.htm 1、怎么用mscomm控件检测modem是否与计算机联接正确?2、如何用mscomm挂断modem与别的电话机间的连接?(已接通) ◆ 02.htm CWinInetConnection---一个封装了WinInet API函数的类 ◆ 03.htm MODEM通讯意外处理 ◆ 04.htm MsComm 文字传输 ◆ 05.htm PING一个IP地址(向它发送一个数据包并等待回应) ◆ 06.htm SMTP协议简介 (Simple Mail Transfer Protocol) ◆ 07.htm VB5.0 中远程数据库的访问 ◆ 08.htm VB下如何编写CRC校验程序 ◆ 09.htm VB中Winsock控制的UDP协议的使用 ◆ 10.htm Visual Basic 6中发送邮件的新方法 ◆ 11.htm Visual Basic通信程序设计 ◆ 12.htm Whois 示例程序 ◆ 13.htm Winsock Terminal示例程序 ◆ 14.htm WinSock断开导致客户端问题 ◆ 15.htm 把Outlook信箱中的附件另存为 ◆ 16.htm 保证连网计算机时间同步 ◆ 17.htm 编写网络寻呼机 ◆ 18.htm 编制自已的电话录音小程序 ◆ 19.htm 程序中如何启动默认的拨号连接 ◆ 20.htm 程序中如何启动默认的拨号连接? ◆ 21.htm 打开器并进入指定网址 ◆ 22.htm 打开一个直接到自己主页的器 ◆ 23.htm 得到用户的IP地址 ◆ 24.htm 电话拨号 ◆ 25.htm 电子邮件的标准格式 (RFC 822) ◆ 26.htm 断开拨号网络的连接 ◆ 27.htm 断开与 Internet 的连接 ◆ 28.htm 发送电子邮件附件 ◆ 29.htm 发送电子邮件附件1 ◆ 30.htm 发送电子邮件附件2 ◆ 31.htm 发送电子邮件附件3 ◆ 32.htm 发送电子邮件附件4 ◆ 33.htm 获得用户网络登录名 ◆ 34.htm 基 于Win95 的VB5 串 口 通 信 程 序 ◆ 35.htm 基于Win95的VB5串口通信程序 ◆ 36.htm 几个设置IE的API ◆ 37.htm 检测运程数据传送的断线 ◆ 38.htm 简单的聊天程序 ◆ 39.htm 简单电子邮件发送程序 ◆ 40.htm 建立拨号联接 ◆ 41.htm 将所有窗口最小化 ◆ 42.htm 利用IE控件访问Internet ◆ 43.htm 利用IE控件设计简易器 ◆ 44.htm 利用Mscomm32控件判断MODEM是否打开,或者正在工作,并且判断拨号时是否遇忙音 ◆ 45.htm 利用TAPI进行电话拨号 ◆ 46.htm 利用VB访问Internet ◆ 47.htm 利用VB设计聊天室 ◆ 48.htm 利用Visual Basic实现无线通讯 ◆ 49.htm 利用Winsock控件实现局域网通信 ◆ 50.htm 强制一个本地或远程NT系统关闭 ◆ 51.htm 取得网卡序列号 ◆ 52.htm 如何从 Internet 上取回某一个网页的內容? ◆ 53.htm 如何检测是否已连接到Internet? ◆ 54.htm 如何利用Winsock控件编写自己的Internet程序 ◆ 55.htm 如何每天抓取 Internet 上某一个网页中的图片来更换桌面的壁纸? ◆ 56.htm 如何启动拨号网路中的连线? ◆ 57.htm 如何使用MSCOMM32.OCX发送大于80H的字符,可否给个示例程序 ◆ 58.htm 如何通过VB获取网卡地址 ◆ 59.htm 如何用VB打开默认器或默认发信程序? ◆ 60.htm 如何用Visual Basic编写小型的网络系统 ◆ 61.htm 如何在程序中启动 NT 的【拨号连接】对话框 ◆ 62.htm 如何在程序中启动 NT 的【拨号连接】对话框? ◆ 63.htm 如何在网页上使用 VB5 制作的 ActiveX 控件? ◆ 64.htm 如何中断【拨号网络连接】 ◆ 65.htm 设计Browser及FTP程序 ◆ 66.htm 设计Chat程序 ◆ 67.htm 设计E-mail程序 ◆ 68.htm 设置计算机名称 ◆ 69.htm 使用VB获得一页的HTML代码 ◆ 70.htm 使用VB收发电子邮件 ◆ 71.htm 使用标签控件调出器 ◆ 72.htm 使用超链接和发送Email ◆ 73.htm 使用器的文件复制对话框 ◆ 74.htm 输入/输出篇 ◆ 75.htm 通过WnetEnumResource函数获得网络资源 ◆ 76.htm 通往 Internet 的捷径 ◆ 77.htm 显示计算机的名称 ◆ 78.htm 用 MAPI 控件实现发送邮件 ◆ 79.htm 用 WinSock 控件下载文件 ◆ 80.htm 用 Winsock 实现聊天的程序 ◆ 81.htm 用Internet Transfer编写FTP程序 ◆ 82.htm 用Sockets发送电子邮件 ◆ 83.htm 用VB 创 建 自 己 的 通 信 程 序 ◆ 84.htm 用VB5.0开发通信软件的技巧 ◆ 85.htm 用VB5开发IE ◆ 86.htm 用VB编写网络寻呼 ◆ 87.htm 用VB编写小型的网络系统 ◆ 88.htm 用VB创建自己的通信程序 ◆ 89.htm 用VB构建Internet的应用[微软提供] ◆ 90.htm 用VB开发标准CGI程序 ◆ 91.htm 用VB实现客户——服务器(TCP IP)编程实例 ◆ 92.htm 用VB实现客户服务器(TCP、IP)编程实例 ◆ 93.htm 用VB实现聊天讨论室和点对点会话 ◆ 94.htm 用VB写一个定时PING某IP的程序 ◆ 95.htm 用VB制作器 ◆ 96.htm 用Visual Basic 5.0设计E-mail程序(MAPI) ◆ 97.htm 用Visual Basic6.0编写客户服务器程序 ◆ 98.htm 用Visual Basic创建FTP组件 ◆ 99.htm 用Visual Basic开发数据库器 ◆ 100.htm 用Visual Basic轻松地设计Browser及Ftp程序 ◆ 101.htm 用Winsock控件发信Email ◆ 102.htm 用Winsock控件实现文件的下载 ◆ 103.htm 用Winsock实现点对点通信 ◆ 104.htm 邮件检查程序(二) ◆ 105.htm 邮件检查程序(一) ◆ 106.htm 在VB程序中怎样挂断拨号网络 ◆ 107.htm 在VB中操纵OLE服务器应用程序 ◆ 108.htm 在VB中利用UDP协议编写聊天程序 ◆ 109.htm 在VB中模拟实现邮件传输 ◆ 110.htm 在VB中如何得到网络中某一台电脑(电脑名)的网卡地质? (自己的) ◆ 111.htm 在VB实现文件上传 ◆ 112.htm 在VB实现文件上载 ◆ 113.htm 在VB中使用UDP协议 ◆ 114.htm 在VB中用Outlook发电子邮件 ◆ 115.htm 在WindowsNT网络中广播消息 ◆ 116.htm 在程序中打开 Internet 拨号连接窗口 ◆ 117.htm 在一个单位内部或通过广域协议(如X.25)互联的行业内部都有几十或上万台计算机互联,用Intranet虽然可以建立聊天室,但实现点对点实时对话却比较困难。本人用Winsock和VB自制了一套聊天室和对话系统,特拿来供同行们参考。 ◆ 118.htm 在应用中集成器 ◆ 119.htm 在桌面上建立一个 Internet 快捷键 ◆ 120.htm 怎样接收电子邮件(POP3协议简介) ◆ 121.htm 怎样用VB得知系统当前是否处于internet链结状态 ◆ 122.htm 制作自己的网络搜索软件 ◆ 123.htm 自动更新工作站的应用程序 ◆ 124.htm 自己的IE——用VB制作器 ◆ 125.htm HTTP协议(学习笔记) ◆ 126.htm HTTP协议四--关于Chunked编码 ◆ 127.htm IE器完全控制 ◆ 128.htm OICQ服务器系统通讯协议 ◆ 129.htm POP3协议的基本命令 ◆ 130.htm RAS API上的其他Function ◆ 131.htm VB + API 获取 IE 的 “代理服务器” 配制 ◆ 132.htm vb调用winInet API接口post数据到指定的url ◆ 133.htm vb开发通信软件 ◆ 134.htm vb设计数据库电子邮件程序(1) ◆ 135.htm vb设计数据库电子邮件程序(2) ◆ 136.htm vb设计数据库电子邮件程序(3) ◆ 137.htm vb设计数据库电子邮件程序(4) ◆ 138.htm vb设计数据库电子邮件程序(5) ◆ 139.htm vb设计数据库电子邮件程序(6) ◆ 140.htm VB实现应用程序在局域网上自动更新 ◆ 141.htm VB邮件检查程序 ◆ 142.htm vb中从域名得到IP及从IP得到域名 ◆ 143.htm VB中检测是否连网 ◆ 144.htm WebClass实现动态WEB编程之理论篇 ◆ 145.htm Winsocket网络编程谈 ◆ 146.htm Winsock编程框架 ◆ 147.htm Winsock错误代码一览表 ◆ 148.htm 程式自动呼叫拨号网络(RAS API) ◆ 149.htm 打开一个超连接 ◆ 150.htm 得到登入windows 的人的id ◆ 151.htm 访问Internet并调用Explorer ◆ 152.htm 非同步文件下载类 ◆ 153.htm 给Outlook的所有用户发送信件 ◆ 154.htm 获得网卡的MAC地址 ◆ 155.htm 基于WinSock的小型网络管理系统及其实现 ◆ 156.htm 检测是否连接到Internet ◆ 157.htm 检测网络是否连通 ◆ 158.htm 建立Web的超链接树形图 ◆ 159.htm 建立你自己的器-Properties ◆ 160.htm 建立你自己的器-查看源文件 ◆ 161.htm 建立你自己的器-打印 ◆ 162.htm 建立你自己的器-打印设置 ◆ 163.htm 建立你自己的器-发送到FontPage ◆ 164.htm 建立你自己的器-发送到Visual Interdev ◆ 165.htm 建立你自己的器-发送到软盘 ◆ 166.htm 建立你自己的器-复制 ◆ 167.htm 建立你自己的器-另存为 ◆ 168.htm 建立你自己的器-全选 ◆ 169.htm 建立你自己的器-页面设置 ◆ 170.htm 开发通信软件的技术与技巧 ◆ 171.htm 开发网络应用的5个技巧 ◆ 172.htm 利用RAS调用在VB6.0中实现拨号上网 ◆ 173.htm 利用VB提取HTML文件中的EMAIL地址 ◆ 174.htm 枚举出局域网上所有网络资源 ◆ 175.htm 面向Internet的开发工具 ◆ 176.htm 你也可以YAI--VB5中Winsock控件的使用 ◆ 177.htm 判断一个文件是否在IE的缓存中 ◆ 178.htm 启动拨号网络中的连接 ◆ 179.htm 浅谈HTTP协议(二)--返回值 ◆ 180.htm 浅谈HTTP协议(一)--结构 ◆ 181.htm 如何利用 WebBrowser 控件显示 .GIF 动画? ◆ 182.htm 如何映射(中断网络磁盘) ◆ 183.htm 设计E-mail的接收部分 ◆ 184.htm 设置器默认网址 ◆ 185.htm 实现端口对端口的聊天 ◆ 186.htm 使用ASP建立Http组件 ◆ 187.htm 使用VB编写纯ASP程序 ◆ 188.htm 使用vb获取网上邻居里的计算机名 ◆ 189.htm 使用VB建立Web Server ◆ 190.htm 使用Visual Basic开发通讯软件 ◆ 191.htm 使用资源工具包执行本地和远程重新启动 ◆ 192.htm 谈谈远程控制中关于搜索、控制计算机的功能 ◆ 193.htm 压缩 Html ◆ 194.htm 用ASP、VB和XML建立互联网应用程序(1) ◆ 195.htm 用ASP、VB和XML建立互联网应用程序(2) ◆ 196.htm 用ASP、VB和XML建立互联网应用程序(3) ◆ 197.htm 用ASP、VB和XML建立互联网应用程序(4) ◆ 198.htm 用COMMUNICATION控件进行数据采集(学习笔记) ◆ 199.htm 用OLE自动化Outlook ◆ 200.htm ActiveX控件用于DHTML开发 ◆ 201.htm 用VB编写ActiveX DLL实现ASP编程 ◆ 202.htm 用VB编写Windows CGI应用程序 ◆ 203.htm 用VB编写标准CGI程序(上) ◆ 204.htm 用VB编写标准CGI程序(下) ◆ 205.htm 用VB编写收发电子邮件程序 ◆ 206.htm 用VB编写网络监控软件 ◆ 207.htm 用VB编写异步多线程下载程序 ◆ 208.htm 用VB导入导出IE器收藏夹 ◆ 209.htm 用VB定制合路器 ◆ 210.htm 用VB和XML建立集中式应用程序 ◆ 211.htm 用VB将Html转换为文本文件 ◆ 212.htm 用VB开发分布式应用 ◆ 213.htm 用VB设计基于代理服务器的网络计费系统 ◆ 214.htm 用VB制作文件下载程序 ◆ 215.htm 用Visual Basic 实现无线通讯 ◆ 216.htm 用Winsock制作一套聊天室和对话系统 ◆ 217.htm 远程共享显示及声音的实现 ◆ 218.htm 远程启动机器ABC API解决方案 ◆ 219.htm 远程启动机器ABC WMI解决方案 ◆ 220.htm 在VB5中利用Winsock和msDNS控件进行 ◆ 221.htm 找出计算机上当前登录入网的用户 ◆ 222.htm 用VB5 Winsock控件创建TCP(IP)客户机 服务器程序

7,788

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧