Chinaunix首页 | 论坛 | 博客
  • 博客访问: 468098
  • 博文数量: 724
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(724)

文章存档

2011年(1)

2008年(723)

我的朋友

分类:

2008-10-13 17:20:23

异步的方法执行
Class Form1 : Inherits Form

  '*** create delegate object to execute method asynchronously
  Private TargetHandler As GetCustomerListHandler _
                           = AddressOf DataAccessCode.GetCustomerList

  '*** create delegate object to service callback from CLR
  Private CallbackHandler As AsyncCallback _
                             = AddressOf MyCallbackMethod

  Sub cmdExecuteTask_Click(sender As Object, e As EventArgs) _
                           Handles cmdExecuteTask.Click
    '*** execute method asynchronously with callback
    TargetHandler.BeginInvoke("CA", CallbackHandler, Nothing)
  End Sub

  '*** CLR calls this method when asynchronous method completes
  '*** note this method runs on worker thread (not the UI thread)
  Sub MyCallbackMethod(ByVal ar As IAsyncResult)
    Dim retval As String()
    retval = TargetHandler.EndInvoke(ar)
    '*** add code here to trigger updating of user interface
  End Sub

End Class

Class DataAccessCode
  Shared Function GetCustomerList(ByVal State As String) As String()
    '*** call across network to DBMS or Web services to retrieve data
    '*** imagine; this method takes several seconds to complete
  End Function
End Class

调用UpdateUI
'*** this method fires at completion of asynchronous call
Sub MyCallbackMethod(ByVal ar As IAsyncResult)
 Try
    '*** retrieve return value from async call
    Dim retval As String()
    retval = TargetHandler.EndInvoke(ar)
    '*** begin process to update UI
    UpdateUI("Task complete", retval)
  Catch ex As Exception
    Dim msg As String
    msg = "Error: " & ex.Message
    UpdateUI(msg, Nothing)
  End Try
End Sub

调用BeginInvoke
Sub UpdateUI(StatusMessage As String, Customers As String())
  '*** switch control over to primary UI thread
  Dim handler As New UpdateUIHandler(AddressOf UpdateUI_Impl)
  Dim args() As Object = {StatusMessage, Customers}
  '*** call BeginInvoke method of Form object
  Me.BeginInvoke(handler, args)
End Sub

Delegate Sub UpdateUIHandler(StatusMessage As String, _
                             Customers As String())

Sub UpdateUI_Impl(StatusMessage As String, Customers As String())
  '*** update user interface controls from primary UI thread
  Me.sbMain.Panels(0).Text = StatusMessage
  Me.lstCustomers.DataSource = Customers
End Sub

在线程间切换
Public Class Form1 : Inherits Form

  '*** create delegate object to execute method asynchronously
  Private TargetHandler As GetCustomerListHandler _
                           = AddressOf DataAccessCode.GetCustomerList

  '*** create delegate object to service callback from CLR
  Private CallbackHandler As AsyncCallback _
                             = AddressOf MyCallbackMethod

  Sub cmdExecuteTask_Click(sender As Object, e As EventArgs) _
                           Handles cmdExecuteTask.Click
    '*** execute method asynchronously with callback
    UpdateUI("Starting task...", Nothing)
    TargetHandler.BeginInvoke("CA", CallbackHandler, Nothing)
  End Sub

  '*** callback method runs on worker thread and not the UI thread
  Sub MyCallbackMethod(ByVal ar As IAsyncResult)
    Try
      Dim retval As String()
      retval = TargetHandler.EndInvoke(ar)
      UpdateUI("Task complete", retval)
    Catch ex As Exception
      Dim msg As String
      msg = "Error: " & ex.Message
      UpdateUI(msg, Nothing)
    End Try
  End Sub
 
  '*** can be called from any method on form to update UI
  Sub UpdateUI(StatusMessage As String, Customers As String())
    '*** check to see if thread switch is required
    If Me.InvokeRequired Then
      Dim handler As New UpdateUIHandler(AddressOf UpdateUI_Impl)
      Dim args() As Object = {StatusMessage, Customers}
      Me.BeginInvoke(handler, args)
    Else
      UpdateUI_Impl(StatusMessage, Customers)
    End If
  End Sub

  '*** delegate used to switch control over to primary UI thread
  Delegate Sub UpdateUIHandler(StatusMessage As String, _
                               Customers As String())

  '*** this method always runs on primary UI thread
  Sub UpdateUI_Impl(StatusMessage As String, Customers As String())
    Me.sbMain.Panels(0).Text = StatusMessage
    Me.lstCustomers.DataSource = Customers
  End Sub

End Class


--------------------next---------------------

阅读(160) | 评论(0) | 转发(0) |
0

上一篇:Figures

下一篇:欢迎阅读我的文章

给主人留下些什么吧!~~