在VB中实现异步调用

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

posted @ 2005-01-04 11:11  greystar  阅读(1896)  评论(0编辑  收藏  举报