Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
Control Class
Control Methods
Invoke Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Control.Invoke Method

Executes a delegate on the thread that owns the control's underlying window handle.

Name Description
Control.Invoke (Delegate) Executes the specified delegate on the thread that owns the control's underlying window handle.

Supported by the .NET Compact Framework.

Control.Invoke (Delegate, Object[]) Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.

Supported by the .NET Compact Framework.

Community Content   What is Community Content?
Add new content RSS  Annotations
Quick and Easy Cross-Thread Control Manipulation via Anonymous Methods      Randolpho ... akcoder   |   Edit   |   Show History

Invoking() a control from a worker thread is common when you're doing multithreading in a windows form. The usual pattern for this goes something like

  private delegate void PrettyMuchUselessDelegate(string text);
  private void WorkerMethod()
  {
  	string result;
  	//
  	// perform some excruciating calculations here to fill the variable 'result'
  	//
  	UpdateUI(result);
  }
    
private void UpdateUI(string text)
  {
  	if(lblResult.InvokeRequired)
  	{
  		lblResult.Invoke(new PrettyMuchUselessDelegate(UpdateUI),new object[] { text }));
  	}
  	else
  	{
  		lblResult.Text = text;
  	}
  }

Lots of code to do one simple thing: lblResult.Text = text. But by using tools available to us in the framework and the C# compiler, we can compress this down to:

  private void WorkerMethod()
  { 
  	string result; 
  	// 
  	// perform some excruciating calculations here to fill the variable 'result' 
  	// 
  	lblResult.Invoke(new MethodInvoker(delegate { lblResult.Text = result; }));
  }

Because we know WorkerMethod is running on a worker thread, we don't bother checking InvokeRequired. We can directly call Invoke on our control, passing a new delegate of type System.Windows.Forms.MethodInvoker. MethodInvoker is a delegate for a method that takes no parameters and returns void. Instead of creating a separate method to invoke, we pass in an anonymous method that sets the text of the label. Using MethodInvoker and anonymous methods helps make the code simpler and more elegant, and what's more important than that?

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker