473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
2 2659
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: polarz | last post by:
I'm having trouble getting items from different classes to display in my textBox. e.g. private void button2_Click(object sender, System.EventArgs e) { someData sd = new someData();...
5
by: user | last post by:
Hello I have Form1 : System.Windows.Forms.Form and some public controls in it: public System.Windows.Forms.ComboBox comboBox1; public System.Windows.Forms.ComboBox comboBox2; in the same...
3
by: Alexander | last post by:
I have a dialog that loads a file from a web server, this is done in an own thread. On finish or failure the thread is supposed to raise an event notifying the dialog. If the WebRequest now raises...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
3
by: Jon Natwick | last post by:
This "Countdown Timer" code works fine with Ie, but I receive an "Error: Form1 is not defined" error with Firefox 1.0.0 and 1.0.1. <body MS_POSITIONING="FlowLayout" onload="InitializeTimer()">...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
6
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second...
5
by: iLL | last post by:
So why is it that we need to use the Invoke method when updating the GUI? The following is the way we are suppose to update GUI components: delegate void textIt(object o); public partial...
1
by: subrat kumarnayak | last post by:
i want to move to form2 on buttonclick at form1 and at the same time i want to close form1
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.