By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Databinding property of object in other thread

P: n/a
I have an object which runs in a sperate thread, from the main thread, and
while running it updates a public property.
In my main form I defined databinding on the property and I would like to
see this databinding update my form property to reflect the value of the
other components property, but it does not happen.

My binding is Databinding.Add("Text",renderEngine,"Framerate");
When the property is initially set, to minus one, the text property is
updated to reflect this, but later changes do not show.
I tried setting a timer, on the main form, to call my binding with
WriteValue(), but that does nothing to better the situation.
If I, in the timer event, just say Text=renderEngine.Framerate.ToString()
then it works fine, but then I loose the nicity of databinding.

What might be the solution to this problem?

Nov 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You would have to modify your renderer to take an implementation of
ISynchronizeInvoke. Then, you can call the Invoke implementation on that
interface. The Control class implements that interface.

If you don't have the interface implementation, you can set the
property directly. Otherwise, you create the delegate and pass it to the
Invoke method on ISynchronizeInvoke.
Thanks for your answer. If I understand you correctly there is no simple and
clear way of using databinding between threads then?
...but.. passing the form, as an ISynchronizeInvoke, is not that big a deal
after all. The renderer receives things such as handle to render window in
the constructor.
I will try your suggestion and see if it makes sence to connect the
renderers properties that strongly to the form.

Nov 2 '07 #2

P: n/a
Hi Jason,
Why not have the threaded object raise a custom event with the info as
the payload.
regards
Bob
On Thu, 1 Nov 2007 20:35:45 +0100, "Jason Wolf" <a@b.dewrote:
>I have an object which runs in a sperate thread, from the main thread, and
while running it updates a public property.
In my main form I defined databinding on the property and I would like to
see this databinding update my form property to reflect the value of the
other components property, but it does not happen.

My binding is Databinding.Add("Text",renderEngine,"Framerate");
When the property is initially set, to minus one, the text property is
updated to reflect this, but later changes do not show.
I tried setting a timer, on the main form, to call my binding with
WriteValue(), but that does nothing to better the situation.
If I, in the timer event, just say Text=renderEngine.Framerate.ToString()
then it works fine, but then I loose the nicity of databinding.

What might be the solution to this problem?
Nov 3 '07 #3

P: n/a
You would have to modify your renderer to take an implementation of
ISynchronizeInvoke. Then, you can call the Invoke implementation on that
interface. The Control class implements that interface.
I tried your suggestion, as I understand it, and It still doesnt quite do
what I want.

The constructor is now
public GfxEngine(IntPtr windowHandle, ISynchronizeInvoke mainThreadInvoker)
: this(windowHandle)
{
this.mainThreadInvoker = mainThreadInvoker;
}

So I keep the ISynchroniceInvoker for later use. I pass the main form in the
constructor.

In my property I have

delegate void setFramerateCallback(int framerate);
public int Framerate
{
get
{
return framerate;
}
private set
{
if (mainThreadInvoker.InvokeRequired)
{
setFramerateCallback sfc = new
setFramerateCallback(setFramerate);
mainThreadInvoker.Invoke(sfc, new object[] { value });
}
else
framerate = value;
}
}

private void setFramerate(int rate)
{
Framerate = rate;
}
The databinding in the main form is simply
DataBindings.Add("Text", gfxEngine, "Framerate");
And it initializes Text to -1 which is the initial framerate of the
renderer.

When I, from my render thread, call the private set for framerate, the
mainThradInvoker.InvokeRequired is true and the delegate is created (it is
done locally for clarity right now) and it is called and sets the property,
without further invocations.

Any hints as to what I am doing wrong here? Why does it not work?
Secondly, can I not skip the extra setFramerate method and someho use a
property directly as a method in the delegate?

Nov 4 '07 #4

P: n/a
Why not have the threaded object raise a custom event with the info as
the payload.
That was my first verion actually. OnFramerateUpdated which the form
subscribed to. I thought it was nicer to just set up databinding one place
and have it worl automatically under the hood. I still think that is
prettiest, but it seems that it makes the calling end somewhat more messy so
perhaps I will return to the event idea

Nov 4 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.