I have a class that I want to make thread-safe and am investigating the ISyncronizeInvo ke interface and wondering just what it will take to implement this interface. So far the basic concept of my code is:
So...where do I go from here? My first thought is that it seems reasonable to do my work in the Invoke function; in the BeginInvoke method, I spin off a thread that calls the Invoke method; in the EndInvoke I would just stop the thread started by my invoke method.
The question is, what goes on in the Invoke method? I can't seem to wrap my head around the fact that the Invoke method doesn't actually have any static purpose except to run the delegate...but what does it do with it?
Maybe I'm just short on caffeine today, but I'm having trouble getting to grips with what exactly I'm supposed to do now that I'm looking at this empty class. If anyone can give me a couple of pointers I'm sure I can figure the rest out for myself.
I understand delegates from the other side of the fence...i.e. calling the Invoke function using the concept:
But on the side of actually implementing the Invoke method, I'm a little bit lost...
I'm grateful for any direction anyone can provide.
Cheers
C# or VB code is fine
Code:
Public Class MyClass1
Implements System.ComponentModel.ISynchronizeInvoke
Private _InvokeRequired As Boolean = False
Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As System.IAsyncResult Implements System.ComponentModel.ISynchronizeInvoke.BeginInvoke
End Function
Public Function EndInvoke(ByVal result As System.IAsyncResult) As Object Implements System.ComponentModel.ISynchronizeInvoke.EndInvoke
End Function
Public Function Invoke(ByVal method As System.Delegate, ByVal args() As Object) As Object Implements System.ComponentModel.ISynchronizeInvoke.Invoke
End Function
Public ReadOnly Property InvokeRequired() As Boolean Implements System.ComponentModel.ISynchronizeInvoke.InvokeRequired
Get
Return _InvokeRequired
End Get
End Property
End Class
The question is, what goes on in the Invoke method? I can't seem to wrap my head around the fact that the Invoke method doesn't actually have any static purpose except to run the delegate...but what does it do with it?
Maybe I'm just short on caffeine today, but I'm having trouble getting to grips with what exactly I'm supposed to do now that I'm looking at this empty class. If anyone can give me a couple of pointers I'm sure I can figure the rest out for myself.
I understand delegates from the other side of the fence...i.e. calling the Invoke function using the concept:
Code:
Public Delegate Sub MyDelegate(ByVal MyParams As Object)
Public Function MyFunction(ByVal MyParams As Object)
If Me.InvokeRequired Then
Me.Invoke(New MyDelegate(AddressOf MyFunction), MyParams)
Else
'Do stuff
End If
End Function
I'm grateful for any direction anyone can provide.
Cheers
C# or VB code is fine
Comment