Results 1 to 10 of 10

Thread: EventHandler AddressOf Invoke

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    EventHandler AddressOf Invoke

    i have a background thread that needs to send data back to the main thread.

    the following code works...

    Code:
    me.Invoke(New EventHandler(AddressOf SendSocketData))
    
    Public Sub SendSocketData()
    ' do this
    end sub
    but i need to send parameters.. .this does not work


    Code:
    me.Invoke(New EventHandler(AddressOf SendSocketData("My Data")))
    
    Public Sub SendSocketData(Data as string)
    ' do this
    end sub
    help!
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: EventHandler AddressOf Invoke

    The data is passed as the second parameter of the Invoke function. [Edit: Okay, it does need to be wrapped in an explicit delegate type... but better to use the general purpose Action(Of String) rather than the specific EventHandler delegate which is designed specifically to handle events]

    vbnet Code:
    1. me.Invoke(New Action(Of String)(AddressOf SendSocketData), "My Data")

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

    Re: EventHandler AddressOf Invoke

    You should not be using the EventHandler delegate. You should be using MethodInvoker, Action, Func or a delegate of your own. Follow the CodeBank link in my signature and check out my thread on Accessing Controls From Worker Threads for an in-depth explanation on how to send data in both directions.
    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    Re: EventHandler AddressOf Invoke

    how can i make this work if the entire block of code is in a module.. not a form?
    Kurt Simons
    [I know I'm a hack but my clients don't!]

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

    Re: EventHandler AddressOf Invoke

    It doesn't matter where the code is. You simply need a reference to a control (a form is control too) that was on the appropriate thread. If that's difficult/impractical then use the SynchronizationConext class. You can find an example of that in my Asynchronous TCP thread in the VB.NET CodeBank.
    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    Re: EventHandler AddressOf Invoke

    "appropriate thread"

    i was trying to get my background thread to send a command back to the main thread to do something
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: EventHandler AddressOf Invoke

    Here is a simple example

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            thrd = New Threading.Thread(AddressOf foo)
            thrd.IsBackground = True
            thrd.Start()
        End Sub
    
        Dim thrd As Threading.Thread
    
        Private Sub foo() 'background thread
            For x As Integer = 1 To 10
                Dim del As New UpdTBdel(AddressOf UpdTB)
                Me.Invoke(del, x) 'update UI
                Threading.Thread.Sleep(250)
            Next
        End Sub
    
        Delegate Sub UpdTBdel(someNumber As Object)
    
        Private Sub UpdTB(someNumber As Object)
            TextBox1.Text = DirectCast(someNumber, Integer).ToString
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    Re: EventHandler AddressOf Invoke

    i am close, but i am working in a module so i cannot use me.invoke
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: EventHandler AddressOf Invoke

    So....

    Code:
        Dim thrd As Threading.Thread
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            thrd = New Threading.Thread(AddressOf foo)
            thrd.IsBackground = True
            thrd.Start(Me)
        End Sub
    and the module

    Code:
    Module Module1
        Public Sub foo(formObject As Object) 'background thread
            Dim f As Form = DirectCast(formObject, Form)
            For x As Integer = 1 To 10
                Dim del As New UpdTBdel(AddressOf UpdTB)
                f.Invoke(del, x, f) 'update UI
                Threading.Thread.Sleep(250)
            Next
        End Sub
    
        Delegate Sub UpdTBdel(someNumber As Object, formObject As Object)
    
        Private Sub UpdTB(someNumber As Object, formObject As Object)
            DirectCast(formObject, Form).Controls("TextBox1").Text = DirectCast(someNumber, Integer).ToString
        End Sub
    End Module
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: EventHandler AddressOf Invoke

    Quote Originally Posted by kurtsimons View Post
    "appropriate thread"

    i was trying to get my background thread to send a command back to the main thread to do something
    Quote Originally Posted by kurtsimons View Post
    i am close, but i am working in a module so i cannot use me.invoke
    If you don't have the UI involved at any point, why do you need to invoke on the UI thread? If you do have the UI involved at some point, you can invoke on to the UI thread at that point.

    Alternatively, as has already been mentioned, get the current synchronisation context from the UI thread, and store it where the worker thread can access it. The worker thread can then use the sync context for invoking. jmc has already pointed you to an example he's written on that.

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