I know how to 'invoke' a method......but now...

I have a control that has a property "Selected"
I would like to change it from different thread...
how can I invoke it?

thx

RE: Can I "Invoke" a Property? by anonymous

anonymous
Tue Feb 10 01:06:05 CST 2004

You have to cover the property by a method - and don´t forget to lock your object during changes by the other threa

Thomas

Re: Can I "Invoke" a Property? by Marc

Marc
Tue Feb 10 05:33:29 CST 2004

>I know how to 'invoke' a method......but now...
>
>I have a control that has a property "Selected"
>I would like to change it from different thread...
>how can I invoke it?

You just assign a new value to it, as if it were a publicly available
field of that object:

YourObject.Selected = <new value>

Of course, you'll have to take care of all the threading and
concurrency issues in addition to this assignment....

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch

Re: Can I "Invoke" a Property? by Stoitcho

Stoitcho
Tue Feb 10 08:47:03 CST 2004

Hi babylon,
As long as the control has been created from UI thread different from the
one changing the property the execution has to be switch to the UI thread
created the control. You can use your control's Invoke method

Example:
Yo need to insolate setting Selected property in separate method. Say the
method has the following prototype

void SetSelected(int index); //this is one possible prototype. you could
have more complex one.

1. decalre and delegate for this prototype

delegate void SetSelectedIndexHandler(int index);

2. Declare the SetSelected method as follows

void SetSelectedIndex(int index)
{
//ctrl is a reference to the control, which selected property you want
to set
if(ctrl.InvokeRequired)
{
ctrl.Invoke(new SetSelectedIndexHandler(SetSelected), new
object[]{index});
}

//Set the control's Selected property here.
ctrl.SelectedIndex = index
}

In this example thread safety is guaranteed by setting the selected property
from the UI thread only.

--
HTH
B\rgds
100

"babylon" <amuro@hotmail.com> wrote in message
news:%232i4iw37DHA.2572@TK2MSFTNGP09.phx.gbl...
> I know how to 'invoke' a method......but now...
>
> I have a control that has a property "Selected"
> I would like to change it from different thread...
> how can I invoke it?
>
> thx
>
>



Re: Can I "Invoke" a Property? by babylon

babylon
Tue Feb 10 19:11:56 CST 2004

ic, thank you!

"Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
news:OIdvsR%237DHA.1948@TK2MSFTNGP12.phx.gbl...
> Hi babylon,
> As long as the control has been created from UI thread different from the
> one changing the property the execution has to be switch to the UI thread
> created the control. You can use your control's Invoke method
>
> Example:
> Yo need to insolate setting Selected property in separate method. Say the
> method has the following prototype
>
> void SetSelected(int index); //this is one possible prototype. you could
> have more complex one.
>
> 1. decalre and delegate for this prototype
>
> delegate void SetSelectedIndexHandler(int index);
>
> 2. Declare the SetSelected method as follows
>
> void SetSelectedIndex(int index)
> {
> //ctrl is a reference to the control, which selected property you want
> to set
> if(ctrl.InvokeRequired)
> {
> ctrl.Invoke(new SetSelectedIndexHandler(SetSelected), new
> object[]{index});
> }
>
> //Set the control's Selected property here.
> ctrl.SelectedIndex = index
> }
>
> In this example thread safety is guaranteed by setting the selected
property
> from the UI thread only.
>
> --
> HTH
> B\rgds
> 100
>
> "babylon" <amuro@hotmail.com> wrote in message
> news:%232i4iw37DHA.2572@TK2MSFTNGP09.phx.gbl...
> > I know how to 'invoke' a method......but now...
> >
> > I have a control that has a property "Selected"
> > I would like to change it from different thread...
> > how can I invoke it?
> >
> > thx
> >
> >
>
>



Re: Can I "Invoke" a Property? by Stu

Stu
Wed Feb 11 05:04:08 CST 2004

On a related note, I think it's a shame you can't have a delegate to a
property (either get or set) in C#. (I think you can do it in IL though, not
that that helps.)

"babylon" <amuro@hotmail.com> wrote in message
news:%232i4iw37DHA.2572@TK2MSFTNGP09.phx.gbl...
> I know how to 'invoke' a method......but now...
>
> I have a control that has a property "Selected"
> I would like to change it from different thread...
> how can I invoke it?
>
> thx
>
>



Re: Can I "Invoke" a Property? by Stoitcho

Stoitcho
Wed Feb 11 08:33:33 CST 2004

Hi Stu,
You can do it in IL because properties are not more then two accessor
methods. As a method it can be used with delegates. However, C# doesn't
support delegates for properties. It doesn't mean you can't do it with C#,
though.
Take a look at the code below

using System;

namespace PropertyDelegate
{
delegate int GetIntAccessorDelegate(); //Deleagte for the Get method
delegate void SetIntAccessorDelegate(int value); //Delegate for the Set
method

//Test class
class Foo
{
private int mVal = 10;

public int Val
{
get
{
return mVal;
}
set
{
mVal = value;
}
}
}

class Class1
{
static Foo mFoo = new Foo();

[STAThread]
static void Main(string[] args)
{

//Creating *get* delegate
GetIntAccessorDelegate getDlgt =
(GetIntAccessorDelegate)Delegate.CreateDelegate(typeof(GetIntAccessorDelegat
e), mFoo, "get_Val");
//Creating *set* delegate
SetIntAccessorDelegate setDlgt =
(SetIntAccessorDelegate)Delegate.CreateDelegate(typeof(SetIntAccessorDelegat
e), mFoo, "set_Val");

Console.WriteLine("mFoo.Val(before) = {0}", getDlgt());
setDlgt(100);
Console.WriteLine("mFoo.Val(after) = {0}", getDlgt());

Console.ReadLine();
}
}
}


--
HTH
B\rgds
100

"Stu Smith" <stuarts@remove.digita.com> wrote in message
news:ePTCH7I8DHA.2712@tk2msftngp13.phx.gbl...
> On a related note, I think it's a shame you can't have a delegate to a
> property (either get or set) in C#. (I think you can do it in IL though,
not
> that that helps.)
>
> "babylon" <amuro@hotmail.com> wrote in message
> news:%232i4iw37DHA.2572@TK2MSFTNGP09.phx.gbl...
> > I know how to 'invoke' a method......but now...
> >
> > I have a control that has a property "Selected"
> > I would like to change it from different thread...
> > how can I invoke it?
> >
> > thx
> >
> >
>
>