我在2K下用VB开发,遇到线程不能执行的问题
现象:在VB中调试时,可以执行,但是一但退出应用就连VB也跟着关闭,如果执行生成的EXE文件,老报“什么什么指令引用什么什么内存,内存不能为read什么什么”的错误。根本不能执行。
模块ServiceModule内容如下:
'线程安全属性数据结构;
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type'这个是用于多线程访问临界资源同步Api的数据结构
Public Type CRITICAL_SECTION
Dummy As Long
End TypeDeclare Function CreateThread Lib "kernel32" (ByVal lpSecurityAttributes _
As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, _
ByVal lpParameter As Long, ByVal dwCreationFlags As Long, _
lpThreadId As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Sub EnterCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION) '进入临界区
Public Declare Sub LeaveCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION) '离开临界区Public FormHandle As Long
'临界数据结构
Public Sect As CRITICAL_SECTIONSub SendMSG()
'Dim dc As Long
'dc = GetDC(formhandle)
While True
Call Console.Send(GetServer, GetUID, GetPWD, GetDataBase)
Call Sleep(GetTimes)
Wend
'Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(Sect)
Call LeaveCriticalSection(Sect)
End SubSub RecvMSG()
'Dim dc As Long
'dc = GetDC(formhandle)
While True
Call Console.Recieve(GetServer, GetUID, GetPWD, GetDataBase)
Call Sleep(500)
Wend
'Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(Sect)
Call LeaveCriticalSection(Sect)
End Sub
form内容如下:
'加载窗体
Private Sub Form_Load()
FormHandle = Console.hwnd
InitSMGPAPI App.Path & "\config\smgpc.ini"
'User是我自己写的ocx,组要是利用winsock监控远程服务器
IsConnect = User.IsConnect(GetServer, GetPort)
If IsConnect = False Then
Unload Me
Exit Sub
End If
End SubPrivate Sub Start_Command_Click()
Dim threadid1 As Long
Dim threadid2 As Long
hThread1 = CreateThread(0, 2000, AddressOf ServiceModule.SendMSG, 0, 0, threadid1)
hThread2 = CreateThread(0, 2000, AddressOf ServiceModule.RecvMSG, 0, 0, threadid2)
End Sub
现象:在VB中调试时,可以执行,但是一但退出应用就连VB也跟着关闭,如果执行生成的EXE文件,老报“什么什么指令引用什么什么内存,内存不能为read什么什么”的错误。根本不能执行。
模块ServiceModule内容如下:
'线程安全属性数据结构;
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type'这个是用于多线程访问临界资源同步Api的数据结构
Public Type CRITICAL_SECTION
Dummy As Long
End TypeDeclare Function CreateThread Lib "kernel32" (ByVal lpSecurityAttributes _
As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, _
ByVal lpParameter As Long, ByVal dwCreationFlags As Long, _
lpThreadId As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Sub EnterCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION) '进入临界区
Public Declare Sub LeaveCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION) '离开临界区Public FormHandle As Long
'临界数据结构
Public Sect As CRITICAL_SECTIONSub SendMSG()
'Dim dc As Long
'dc = GetDC(formhandle)
While True
Call Console.Send(GetServer, GetUID, GetPWD, GetDataBase)
Call Sleep(GetTimes)
Wend
'Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(Sect)
Call LeaveCriticalSection(Sect)
End SubSub RecvMSG()
'Dim dc As Long
'dc = GetDC(formhandle)
While True
Call Console.Recieve(GetServer, GetUID, GetPWD, GetDataBase)
Call Sleep(500)
Wend
'Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(Sect)
Call LeaveCriticalSection(Sect)
End Sub
form内容如下:
'加载窗体
Private Sub Form_Load()
FormHandle = Console.hwnd
InitSMGPAPI App.Path & "\config\smgpc.ini"
'User是我自己写的ocx,组要是利用winsock监控远程服务器
IsConnect = User.IsConnect(GetServer, GetPort)
If IsConnect = False Then
Unload Me
Exit Sub
End If
End SubPrivate Sub Start_Command_Click()
Dim threadid1 As Long
Dim threadid2 As Long
hThread1 = CreateThread(0, 2000, AddressOf ServiceModule.SendMSG, 0, 0, threadid1)
hThread2 = CreateThread(0, 2000, AddressOf ServiceModule.RecvMSG, 0, 0, threadid2)
End Sub
2、对于exe运行出错的问题,你可以将你的防火墙关闭了再试一下。是不是金山毒霸?
Norton和ISA!
和这有何关系?
hThread1 = CreateThread(500, 2000, AddressOf ServiceModule.SendMSG, 0, 0, threadid1)
hThread2 = CreateThread(500, 2000, AddressOf ServiceModule.RecvMSG, 0, 0, threadid2)
前面两个参数该怎么传呢?
hThread2 = CreateThread(0, 0, byval AddressOf ServiceModule.RecvMSG, 0, 0, threadid2)or :
public declare function CreateThread lib "Kernel32" (pSa as any,byval dwStatck as long,byval lpFun as long,lpParam as any,byval dwFlag as long,threadID as long)as longhThread1 = CreateThread(0, 0, AddressOf ServiceModule.SendMSG, 0, 0, threadid1)
hThread2 = CreateThread(0, 0, AddressOf ServiceModule.RecvMSG, 0, 0, threadid2)note:byval lpFun as long VB对县城支持不好
出现很多问题。两个解决方案:
1.升级到VB.NET, VB.NET支持自由线程
2.用VC实现.