|
-
Dec 15th, 2011, 07:32 PM
#1
Thread Starter
Fanatic Member
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!]
-
Dec 15th, 2011, 07:45 PM
#2
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")
-
Dec 15th, 2011, 10:34 PM
#3
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.
-
Dec 16th, 2011, 08:54 AM
#4
Thread Starter
Fanatic Member
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!]
-
Dec 16th, 2011, 09:23 AM
#5
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.
-
Dec 16th, 2011, 10:36 AM
#6
Thread Starter
Fanatic Member
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!]
-
Dec 16th, 2011, 10:47 AM
#7
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
-
Dec 16th, 2011, 11:42 AM
#8
Thread Starter
Fanatic Member
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!]
-
Dec 16th, 2011, 12:37 PM
#9
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
-
Dec 18th, 2011, 06:03 PM
#10
Re: EventHandler AddressOf Invoke
 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
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|