Home All Groups Group Topic Archive Search About

Accessing controls from another thread

Author
3 Feb 2006 9:17 AM
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.

Author
3 Feb 2006 9:45 AM
NickP
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)

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

Nick.

Show quoteHide quote
"Nick Pateman" <a@a.com> wrote in message
news:%23FBvsKKKGHA.3960@TK2MSFTNGP09.phx.gbl...
> 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.
>