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!
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:
me.Invoke(New Action(Of String)(AddressOf SendSocketData), "My Data")
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.
Re: EventHandler AddressOf Invoke
how can i make this work if the entire block of code is in a module.. not a form?
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.
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
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
Re: EventHandler AddressOf Invoke
i am close, but i am working in a module so i cannot use me.invoke
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
Re: EventHandler AddressOf Invoke
Quote:
Originally Posted by
kurtsimons
"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
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.