[RESOLVED] Threading issue using a dll (illegal cross thread call)
Ok, here is my question. I created a dll that is essentially a memory monitor. It is constantly monitoring a specific memory location checking for changes and when it finds one it raises an event. The searches are all done in a thread created by the dll. Here is my issue. In the forms application using the dll, I want to write data to a text box when the event is called. So I am able to do this as long as I create a Delegate handler and invoke it before writing to the textbox otherwise I get an illegal cross thread call error.
My question is this... Is it possible to invoke the calling thread from within the dll so that I do not have to in the forms application that is using the dll. I have tried quite a few methods and have yet to be successful at it.. I am reaching the point of thinking it isn't possible. If anyone has any insight on this one I would greatly appreciate it.
Thanks
bmahler
Re: Threading issue using a dll (illegal cross thread call)
Does the DLL know anything about the control? Given that you're supposed to call the Invoke method of the control you're accessing then it really depends on that.
That said, you don't actually have to call the Invoke method of that control. If your DLL has a reference to the form the control is on then it could call that form's Invoke method. As long as the control whose Invoke method is called was created on the same thread as the control you want to access then it will be fine. You could even create your own control, as long as it was created on the correct thread. That's pretty dodgy though.
There is a proper way to do this, as it's already done in the Framework in several places. The FileSystemWatcher and Timers.Timer components both have a SynchronizingObject property. If you assign a control to that property then the component will use it to marshal its events to the thread that created that object. Otherwise they will raise them on a worker thread. You should add a SynchronizingObject to your own class and do the same thing. It would look something like this:
vb.net Code:
Public Class SomeClass
Private _synchronizingObject As System.ComponentModel.ISynchronizeInvoke
Public Property SynchronizingObject() As System.ComponentModel.ISynchronizeInvoke
Get
Return Me._synchronizingObject
End Get
Set(ByVal value As System.ComponentModel.ISynchronizeInvoke)
Me._synchronizingObject = value
End Set
End Property
Public Event SomeEvent As EventHandler
Private Delegate Sub SomeEventInvoker(ByVal e As EventArgs)
Protected Overridable Sub OnSomeEvent(ByVal e As EventArgs)
If Me.SynchronizingObject IsNot Nothing AndAlso Me.SynchronizingObject.InvokeRequired Then
'Marshal the call to the thread that owns the synchronizing object.
Me.SynchronizingObject.Invoke(New SomeEventInvoker(AddressOf OnSomeEvent), _
New Object() {e})
Else
RaiseEvent SomeEvent(Me, e)
End If
End Sub
End Class
Now when you create an instance of that class you can assign your form or some control to the SynchronizingObject property and then it will raise its events on the correct thread.
Re: Threading issue using a dll (illegal cross thread call)
Thank you very much for the info. I will give this a shot.
Re: Threading issue using a dll (illegal cross thread call)
That worked perfectly. thanks again for the info.
:thumb: