Advertisement
Guest User

Synchronize Invoke

a guest
Feb 7th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.23 KB | None | 0 0
  1.     Public Event DataArrival(ByVal sender As Object, ByVal Data As String)
  2.  
  3.     Private Sub RaiseEventSafe( _
  4.             ByVal ev As System.Delegate, _
  5.             ByRef args() As Object)
  6.         Dim bFired As Boolean
  7.         If ev IsNot Nothing Then
  8.             For Each singleCast As System.Delegate In _
  9.                     ev.GetInvocationList()
  10.                 bFired = False
  11.                 Try
  12.                     Dim syncInvoke As ISynchronizeInvoke = _
  13.                         CType(singleCast.Target, ISynchronizeInvoke)
  14.                     If syncInvoke IsNot Nothing _
  15.                           AndAlso syncInvoke.InvokeRequired Then
  16.                         bFired = True
  17.                         syncInvoke.Invoke(singleCast, args)
  18.                     Else
  19.                         bFired = True
  20.                         singleCast.DynamicInvoke(args)
  21.                     End If
  22.                 Catch ex As Exception
  23.                     If Not bFired Then singleCast.DynamicInvoke(args)
  24.                 End Try
  25.             Next
  26.         End If
  27.     End Sub
  28.  
  29.     Private Sub ThrowDataArrival(ByVal sender As Object, ByVal Data As String)
  30.         RaiseEventSafe(DataArrivalEvent, New Object() {sender, Data})
  31.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement