本帖最后由 submariner 于 2022-6-27 12:38 编辑
我试着写了一段测试程序,总是发生闪退,能帮看看吗?
不知怎的,无法上传附件,贴主要代码如下:
窗体上只有text1~text6共六个TextBox控件,command1和command2两个按钮控件
点击command2按钮时,程序会无任何错误提示,后面就直接结束了
[Visual Basic] 纯文本查看 复制代码 '模块代码
Option Explicit
Public Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes 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 Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub initTlsFake Lib "tlsfake.dll" ()
Public Type lpParaIn
beginLong As Long
endLong As Long
RetTLSecond As Double
RetS As Double
End Type
Public Sub mx(i0 As Long, ii As Long, ByRef RetTls As Double, ByRef RetS As Double)
'功能:从i0次计算到ii次,传地址返回本阶段用时秒数RetTls和本阶段计算结果RetS
Dim S As Double, xs As Double, i As Long
Dim Tb As Long, Te As Long, Tl As Long, Tls As Double
Tb = GetTickCount()
S = 0
For i = i0 To ii
S = S + Sin(i * 0.0172222) '主要计算累加Sin(i * 0.0172222)
Next i
Te = GetTickCount
Tl = Te - Tb
Tls = Tl / 1000#
RetTls = Tls
RetS = S
End Sub
Public Sub mtst(Iloop As Long, ByRef S As Double, ByRef Tls As Double)
'功能:从1到ILoop次计算,4线程,传地址返回全程用时秒数Tls和本阶段计算结果S
Dim i As Long, j As Long, k As Long
Dim lpPara(0 To 3) As lpParaIn
j = Iloop / 4
For i = 0 To 3
lpPara(i).beginLong = 1 + i * j
lpPara(i).endLong = (i + 1) * j
Next i
lpPara(3).endLong = Iloop '防止不能被整除,修正末尾
Call initTlsFake
For i = 0 To 3
CreateThread 0, 0, AddressOf mx, VarPtr(lpPara(i)), 0, 0
Sleep 8000
Next
Sleep 8000
S = 0
Tls = 0
For i = 0 To 3
S = S + lpPara(i).RetS
Tls = Tls + lpPara(i).RetTLSecond
Next i
End Sub
[Visual Basic] 纯文本查看 复制代码 ’窗体代码
Option Explicit
Private Sub Command1_Click() '单线程计算结果及用时
Dim ii As Long, S As Double, xs As Double, i As Long
Dim Tb As Long, Te As Long, Tl As Long, Tls As Double
ii = Val(Text1)
Tb = GetTickCount()
S = 0
DoEvents
Command1.Caption = "计算中...请等几秒"
DoEvents
For i = 1 To ii
S = S + Sin(i * 0.0172222) '主要计算累加Sin(i * 0.0172222)
Next i
Te = GetTickCount
Tl = Te - Tb
Tls = Tl / 1000#
Text3 = Str(S)
Text4 = Str(Tls) + " s"
DoEvents
Command1.Caption = "一般的VB计算方法"
DoEvents
End Sub
Private Sub Command2_Click() '多线程计算及结果
Dim ii As Long, S As Double
Dim Tb As Long, Tls As Double
ii = Val(Text1)
Tb = GetTickCount()
Call mtst(ii, S, Tls)
Text5 = Str(S)
Text6 = Str(Tls) + " s"
End Sub
|