How a thread can change a control?

J

John

I am programming an HP iPAQ 1950, using VB .NET. I have two threads, the
main thread and thread #2. I need thread #2 to be able to change a
control on a form. My code runs but does not change the control. I have
tried several permutations but nothing seems to work. Below is sample
code. Note that my delegate uses the EventHandler class because this is
what the help files said I needed to use. Thanks for your help.

--- Code in Module1 (thread 2)
frmChemScanner.CallChangeHdrColor()

--- Code in form frmChemScanner (main thread)
Public Sub CallChangeHdrColor()
Dim e As New EventArgs
Dim o As Object

Dim d As New EventHandler(AddressOf ChangeHdrColor)
Me.Invoke(d, New Object() {o, e})
End Sub
Sub ChangeHdrColor(ByVal sender As Object, ByVal e As EventArgs)
labDeleteMode.Visible = True
End Sub

John
P.S. Please don't respond to the return address.
 
G

Guest

Just like with the desktop, you *must* use Control.Invoke to marhal back to
the primary thread before affecting any UI element.
 
J

John

Chris,

You mean I need to do the following?

Dim d As New EventHandler(AddressOf ChangeHdrColor)
frmChemScanner.labDeleteMode.Invoke(d, New Object() {o, e})

John

ctacke[@]opennetcf said:
Just like with the desktop, you *must* use Control.Invoke to marhal back to
the primary thread before affecting any UI element.
 
G

Guest

I mean this:

http://msdn2.microsoft.com/en-us/library/aa446540.aspx#netcfmultithreadedapp_topic3


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


John said:
Chris,

You mean I need to do the following?

Dim d As New EventHandler(AddressOf ChangeHdrColor)
frmChemScanner.labDeleteMode.Invoke(d, New Object() {o, e})

John

ctacke[@]opennetcf said:
Just like with the desktop, you *must* use Control.Invoke to marhal back
to
the primary thread before affecting any UI element.
John said:
I am programming an HP iPAQ 1950, using VB .NET. I have two threads, the
main thread and thread #2. I need thread #2 to be able to change a
control on a form. My code runs but does not change the control. I have
tried several permutations but nothing seems to work. Below is sample
code. Note that my delegate uses the EventHandler class because this is
what the help files said I needed to use. Thanks for your help.

--- Code in Module1 (thread 2)
frmChemScanner.CallChangeHdrColor()

--- Code in form frmChemScanner (main thread)
Public Sub CallChangeHdrColor()
Dim e As New EventArgs
Dim o As Object

Dim d As New EventHandler(AddressOf ChangeHdrColor)
Me.Invoke(d, New Object() {o, e})
End Sub
Sub ChangeHdrColor(ByVal sender As Object, ByVal e As EventArgs)
labDeleteMode.Visible = True
End Sub

John
P.S. Please don't respond to the return address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top