Results 1 to 4 of 4

Thread: [RESOLVED] Threading issue using a dll (illegal cross thread call)

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Resolved [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
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class SomeClass
    2.  
    3.     Private _synchronizingObject As System.ComponentModel.ISynchronizeInvoke
    4.  
    5.     Public Property SynchronizingObject() As System.ComponentModel.ISynchronizeInvoke
    6.         Get
    7.             Return Me._synchronizingObject
    8.         End Get
    9.         Set(ByVal value As System.ComponentModel.ISynchronizeInvoke)
    10.             Me._synchronizingObject = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Event SomeEvent As EventHandler
    15.  
    16.     Private Delegate Sub SomeEventInvoker(ByVal e As EventArgs)
    17.  
    18.     Protected Overridable Sub OnSomeEvent(ByVal e As EventArgs)
    19.         If Me.SynchronizingObject IsNot Nothing AndAlso Me.SynchronizingObject.InvokeRequired Then
    20.             'Marshal the call to the thread that owns the synchronizing object.
    21.             Me.SynchronizingObject.Invoke(New SomeEventInvoker(AddressOf OnSomeEvent), _
    22.                                           New Object() {e})
    23.         Else
    24.             RaiseEvent SomeEvent(Me, e)
    25.         End If
    26.     End Sub
    27.  
    28. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Threading issue using a dll (illegal cross thread call)

    Thank you very much for the info. I will give this a shot.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Threading issue using a dll (illegal cross thread call)

    That worked perfectly. thanks again for the info.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width