Accessing controls from another thread

  • Thread starter Thread starter Nick Pateman
  • Start date Start date
N

Nick Pateman

Hi there,

Previously in 1.1 .NET Framework if you were to access a control from
another thread you would not recieve an exception by default, instead it was
down to you and/or the control to prevent anything untoward happening.

Anyway, in 2.0 an exception is thrown the second you attempt to do this.
So I'm replacing large chunks of code to remove this problem. The only
thing is I would like to check that I am using the correct procedure.

I am calling the invoke method of the form that created the control.
The only problem here is that if the method requires parameters I need to
pass them in an array of objects...

Private sub changeControlsProperties(Byval iArgs() as Object)
Dim pStrString as String = CStr(iArgs(0))
Dim pIntInteger as Integer = CInt(IArgs(1))
... so on and so fourth and such like ...
End Sub

This doesn't seem like a very nice way to be calling a method so I'm
wondering if anyone knows of any other techniques for handling this "cross
threading" issue.

Nick.
 
Hi again,

What I have ended up doing is strictly declaring the delegate methods,
although this isn't exactly what I was after it does what is required.

---------------

'//Declaration space

Delegate Sub changeControlPropertiesDelegate(Byval iString as String,
Byval iInteger as Integer)

Private cDelChangeControlProperties as New
changeControlPropertiesDelegate(AddressOf changeControlProperties)

Private sub changeControlProperties(Byval iString as String, Byval
iInteger as Integer)
'//Apply properties to control
Exit Sub

'//From some other thread
Dim pObjParams() as Object = {"This is a test", 1001)
Call Invoke(cDelChangeControlProperties, pObjParams)
 
Back
Top