Can I "Invoke" a Property?

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

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
 
You have to cover the property by a method - and don´t forget to lock your object during changes by the other threa

Thomas
 
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
 
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.
 
ic, thank you!

Stoitcho Goutsev (100) said:
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 said:
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
 
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.)
 
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();
}
}
}
 
Back
Top