读书人

多线程有关问题!断点调试的时候没有有

发布时间: 2012-04-11 17:42:33 作者: rapoo

多线程问题!断点调试的时候没有问题,生成EXE文件直接运行就挂了!
断点调试的时候,都很正常,都可以运行到FORM_UNLOAD里。但是生成EXE,直接运行,马上就出错了!
这是什么原因啊???拜请各位大虾指点!!!

*************************************
'FORM2
*************************************
Private Sub cmd_Check_Click()
'Application.ProcessMessages; // 让其他控件能获取消息
Dim lthreadhandle As Long
Dim dwStackSize As Long
Dim dwCreationFlags As Long
Dim lpThreadId As Long
Dim lpParameter As Long
Dim myNull As Long
Dim lpfnBasFunc As Long
myNull = 0& 'create a null pointer
dwStackSize = 0 '0表示用exe stack size
dwCreationFlags = 4 '用4表示初始化后先不激活,让别人来激活.

Dim str_str1 As String
Dim count As Long

If gcn Is Nothing Then
Else
If gcn.State = 1 Then
gcn.Close
End If
Set gcn = Nothing
End If
Set gcn = New ADODB.Connection

If grs Is Nothing Then
Else
If grs.State = 1 Then
grs.Close
End If
Set grs = Nothing
End If
Set grs = New ADODB.Recordset

gcn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Txt_File.Text + ";"
gcn.Open

str_str1 = "select * from [TB1]"
grs.CursorLocation = adUseClient
grs.Open str_str1, gcn, adOpenKeyset, adLockPessimistic

count = grs.RecordCount
If count <> 0 Then
lthreadhandle = CreateThread(myNull, dwStackSize, AddressOf opDatabase1, myNull, dwCreationFlags, lpThreadId)
If lthreadhandle <> myNull Then
ResumeThread lthreadhandle
End If
End If

End Sub

Private Sub Cmd_Open_Click()
On Error Resume Next
dialog_Window.CancelError = True
dialog_Window.filename = ""
dialog_Window.Flags = cdlOFNFileMustExist
dialog_Window.Filter = "Microsoft Access(*.mdb)|*.mdb"
dialog_Window.ShowOpen
If Err > 0 Then
Exit Sub
End If

Dim filename As String
Dim iRet As Integer
filename = dialog_Window.filename
iRet = InStr(1, filename, ".mdb", vbTextCompare)

If iRet = 0 Then
filename = filename + ".mdb"
dialog_Window.filename = filename
filename = Dir(filename)
If filename = Null Then
filename = dialog_Window.filename + "l"
dialog_Window.filename = filename
End If
End If

Txt_File.Text = filename
End Sub

Private Sub Form_Unload(Cancel As Integer)

If grs Is Nothing Then
Else
If grs.State = 1 Then
grs.Close
End If
Set grs = Nothing
End If

If gcn Is Nothing Then
Else
If gcn.State = 1 Then
gcn.Close
End If
Set gcn = Nothing
End If


Fok = LceEndSession(SessionId, 0)
End Sub

********************************************
'THREAD MODEL
********************************************
Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long


Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Global gcn As ADODB.Connection
Global grs As ADODB.Recordset

Public Sub opDatabase1()

Dim I As Integer

I = 0
While grs.EOF <> True

Sleep 2000

I = I + 1
grs.MoveNext
If I > 10 Then
GoTo end1
End If

Form2.lab_Count.Caption = Str(I)

Wend
end1:
Exit Sub
End Sub



[解决办法]
到现在为止我也没有看到过VB比较稳定的多线程开发的代码. 用VB 开发多线程还是放弃吧.
当然把多线程的代码写在VC的DLL中.然后提供接口给VB 调用到是不错的选择.

[解决办法]
是的! 就像重载消息函数一样 处理多线程等 VB是不稳定的 很容易退出整个程序.
[解决办法]
http://www.m5home.com/blog/article.asp?id=54

http://topic.csdn.net/u/20080225/00/4a70c6f4-0a8b-4912-bb6a-74d856460f54.html

如何在VB6里面实现稳定的多线程
[解决办法]
老实说,还真的从来没有遇到过非要用多线程才能解决不可的问题。


[解决办法]
换一个编译模式看看
把它编译成P-CODE试试

读书人网 >VB

热点推荐