VB本身是不支持多线程的。但是VB中的部件有进程内及进程外的区分,因此这里我使用进程外部件来实现异步调用。
服务器端代码,新建一个ActiveEXE工程,加入一个窗体,窗体中存放一个Timer控件
添加一个类模块。代码如下
Private WithEvents m_Timer As Timer
Public Event MyTaskResult(result As Long)
Private j As Long
Private Sub Class_Initialize()
Set m_Timer = Form1.Timer1
End Sub
Private Sub m_Timer_Timer()
m_Timer.Enabled = False
Dim i As Long
Dim r As Long
For i = 0 To j
r = r + i
Next
RaiseEvent MyTaskResult(r)
End Sub
Public Sub MyBigTask(i As Long)
m_Timer.Enabled = True
m_Timer.Interval = 10
j = i
End Sub
客户端
Private WithEvents longTask As AsyncServerTest.MyAsyncCls
Private Sub Command1_Click()
Set longTask = New AsyncServerTest.MyAsyncCls
longTask.MyBigTask (Text1.Text)
End Sub
Private Sub longTask_MyTaskResult(result As Long)
Text2.Text = result
MsgBox "long task finished"
End Sub
博客指出VB本身不支持多线程,可利用进程外部件实现异步调用。介绍了服务器端代码,需新建ActiveEXE工程,加入窗体和Timer控件并添加类模块;还给出客户端代码,通过点击Command1触发任务,任务完成后显示结果并弹出提示框。
1354

被折叠的 条评论
为什么被折叠?



