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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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...
 
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.
 
Back
Top