Option Strict On
Imports System.Threading
Imports System.Runtime.CompilerServices
'Imagine this class to be a class used
'to represent each device retrieved from the database
Public Class Device
Private _DeviceID As Integer
Private _DeviceName As String
Private _SomeDeviceProperty As Integer
Private g_bPingingAsync As Boolean = False
Private context As SynchronizationContext = SynchronizationContext.Current
Public Event PingProgressChanged As EventHandler(Of PingProgressChangedEventArgs)
Protected Overridable Sub OnPingProgressChanged(ByVal e As PingProgressChangedEventArgs)
RaiseEvent PingProgressChanged(Me, e)
End Sub
Public Event PingProgressCompleted As EventHandler
Protected Overridable Sub OnPingProgressCompleted()
RaiseEvent PingProgressCompleted(Me, New EventArgs)
End Sub
Sub New()
End Sub
Sub New(ByVal devName As String, ByVal devID As Integer, ByVal someProp As Integer)
MyClass.New()
Me.DeviceID = devID
Me.DeviceName = devName
Me.SomeDeviceProperty = someProp
End Sub
Public Property DeviceID() As Integer
Get
Return _DeviceID
End Get
Set(ByVal value As Integer)
_DeviceID = value
End Set
End Property
Public Property DeviceName() As String
Get
Return _DeviceName
End Get
Set(ByVal value As String)
_DeviceName = value
End Set
End Property
Public Property SomeDeviceProperty() As Integer
Get
Return _SomeDeviceProperty
End Get
Set(ByVal value As Integer)
_SomeDeviceProperty = value
End Set
End Property
Public Sub PingDevice()
InternalPingDevice()
End Sub
Public Sub PingDeviceAsync()
If g_bPingingAsync Then
Throw New Exception("Alreadying pinging asyncrounously")
End If
g_bPingingAsync = True
ThreadPool.QueueUserWorkItem(AddressOf InternalPingDevice, Nothing)
End Sub
Private Sub InternalPingDevice()
InternalPingDevice(Nothing)
End Sub
Private Sub InternalPingDevice(ByVal state As Object)
Dim i As Integer = 0
Dim e As PingProgressChangedEventArgs
'Imagine this For...Next loop is your pinging process
'----------------------------------------------------
For i = 0 To Me.SomeDeviceProperty
Thread.Sleep(200)
e = New PingProgressChangedEventArgs(CInt((i / Me.SomeDeviceProperty) * 100))
If context Is Nothing Then
OnPingProgressChanged(e)
Else
context.Invoke(New Action(Of PingProgressChangedEventArgs)(AddressOf OnPingProgressChanged), e)
'context.Send(New SendOrPostCallback(AddressOf OnPingProgressChanged), e)
End If
Next
g_bPingingAsync = False
If context Is Nothing Then
OnPingProgressCompleted()
Else
'context.Send(New SendOrPostCallback(AddressOf OnPingProgressCompleted), Nothing)
context.Invoke(New Action(AddressOf OnPingProgressCompleted))
End If
End Sub
End Class
Public Class PingProgressChangedEventArgs
Inherits EventArgs
Private _progress As Integer
Sub New(ByVal progressPercent As Integer)
Me.Progress = progressPercent
End Sub
Public Property Progress() As Integer
Get
Return _progress
End Get
Set(ByVal value As Integer)
_progress = value
End Set
End Property
End Class
Public Module Extensions
<Extension()> _
Public Sub Invoke(ByVal sc As SynchronizationContext, ByVal del As [Delegate], ByVal ParamArray args() As Object)
sc.Send(New SendOrPostCallback(AddressOf SendOrPost), New SendOrPostState With {.Args = args, .DelegateToInvoke = del})
End Sub
Private Sub SendOrPost(ByVal state As Object)
Dim st As SendOrPostState = DirectCast(state, SendOrPostState)
st.DelegateToInvoke.DynamicInvoke(st.Args)
End Sub
Private Class SendOrPostState
Public Args() As Object
Public DelegateToInvoke As [Delegate]
End Class
End Module