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

Form1.TreeView1.Invoke(...) and Form1.Invoke(...)

P: n/a
Tom
Hi Everybod

I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate for all of my controls in the Form and dont define one delegate for each control. Some thing like to have a switch case in that delegate fucntion and passing control name to it..
what is the impact of this method in comparison with Form1.TreeView1.Invoke(...

Thanks
To

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Tom,

Whether you use Form1.Invoke, Form1.ThreeView1.Invoke or
Form1.XXXXctrl.Invoke the efect will be exactly the same. It is not
important which control you use to call Invoke. What is important is the
thread that control belongs to. That is the delegate passed to Invoke method
will be executed from the thread created the control. As long as ThreeView1,
Form1, XXXXctrl all belongs to the same thread (otherwise you cannot add the
controls to the form) it doesn't really matter which one you are going to
use for calling the Invoke method.

Seconly, the metod used with the delegate passed to the Invoke method don't
have to be member of the contol or form class. It can be member of any
class. That method, though, will be executed from the thread created the
form or the control, which Invoke method was called. Thus, you don't have to
have different methods for all control.

--
HTH
B\rgds
100

"Tom" <an*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
Hi Everybody

I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I
want to have just one delegeate for all of my controls in the Form and dont
define one delegate for each control. Some thing like to have a switch case
in that delegate fucntion and passing control name to it... what is the impact of this method in comparison with Form1.TreeView1.Invoke(...)
Thanks,
Tom

Nov 15 '05 #2

P: n/a
Why not use multicast delegates and cross-thread invoke:

void CrossThreadInvokeSingle(Delegate delegate, Args args)
{
Control ctl = delegate.Target as Control;
if (ctl != null)
ctl.Invoke(delegate, args);
else
delegate.DynamicInvoke(args);
}

void CrossThreadInvokeMulti(MultiCastDelegate delegate, Args args)
{
foreach (Delegate item in delegate.GetInvocationList())
{
CrossThreadInvokeSingle(item, args);
}
}

Delegate, MultiCastDelegate are part of CRL.
Args stands for any arguments you may wish to pass to delegate call.

In this case you can subscribe to the event in your form and any number of child controls and call without penalty
and explicit knowledge of what is being called. E.g.

class MyForm : Form
{
void UpdateControls() {
DoUpdate();
}

MyForm() {
OtherThreadObject.OnFinish += new MyDelegate(UpdateControls);
}
}

class OtherThreadClass {
public event MyDelegate OnFinish;

private void InvokeOnFinish() {
if (OnFinish != null)
CrossThreadInvokeMulti(OnFinish);
}
}

OtherThreadClass does not know about form or any subscriber, the only limitation that thread-tied handlers should
be declared in control descendants.

This works great when you have single UI thread with any number of workers.

Tom wrote:
Hi Everybody

I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate for all of my controls in the Form and dont define one delegate for each control. Some thing like to have a switch case in that delegate fucntion and passing control name to it...
what is the impact of this method in comparison with Form1.TreeView1.Invoke(...)

Thanks,
Tom


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.