G
Guest
I am having trouble getting events fired from threads to call back on the
original thread. I have an non-UI "sub" assembly that periodically goes out
and retrieves information. When it finds new or changed information it fires
events to whoever is listening (could be another sub-assembly or a WinForm
application). I am currently using the code I pieced together below to fire
the events:
Public Shared Sub Fire(ByVal del As [Delegate], ByVal ParamArray args() As
Object)
Dim temp As [Delegate] = del
If temp Is Nothing Then
Return
End If
Dim delegates As [Delegate]() = temp.GetInvocationList()
For Each sink As [Delegate] In delegates
Try
Dim invokeChecker As System.ComponentModel.ISynchronizeInvoke =
sink.Target
If invokeChecker.InvokeRequired Then
invokeChecker.BeginInvoke(sink, args)
Else
sink.DynamicInvoke(args)
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Next sink
End Sub
The problem with this code is the "Dim invokeChecker" line fails if the
event was not created by a WinForm. I can't seem to find a way to invoke an
event on a non-UI thread (which most of the threads in my applications are).
If I just put "sink.DynamicInvoke(args)" in the Catch, then when I am using a
UI, the UI always tells me that Invoke is required when it gets the event.
I thought maybe implementing ISynchronizeInvoke in my classes that have
events might work, but I can't find any sample code on what to do in the
methods, especially InvokeRequired.
Any help would be greatly appreciated!
David McCarter
original thread. I have an non-UI "sub" assembly that periodically goes out
and retrieves information. When it finds new or changed information it fires
events to whoever is listening (could be another sub-assembly or a WinForm
application). I am currently using the code I pieced together below to fire
the events:
Public Shared Sub Fire(ByVal del As [Delegate], ByVal ParamArray args() As
Object)
Dim temp As [Delegate] = del
If temp Is Nothing Then
Return
End If
Dim delegates As [Delegate]() = temp.GetInvocationList()
For Each sink As [Delegate] In delegates
Try
Dim invokeChecker As System.ComponentModel.ISynchronizeInvoke =
sink.Target
If invokeChecker.InvokeRequired Then
invokeChecker.BeginInvoke(sink, args)
Else
sink.DynamicInvoke(args)
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Next sink
End Sub
The problem with this code is the "Dim invokeChecker" line fails if the
event was not created by a WinForm. I can't seem to find a way to invoke an
event on a non-UI thread (which most of the threads in my applications are).
If I just put "sink.DynamicInvoke(args)" in the Catch, then when I am using a
UI, the UI always tells me that Invoke is required when it gets the event.
I thought maybe implementing ISynchronizeInvoke in my classes that have
events might work, but I can't find any sample code on what to do in the
methods, especially InvokeRequired.
Any help would be greatly appreciated!
David McCarter