ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

SafeInvoke

Properly invokes an action if it is required. Best way to handle events and threaded operations on a form.

Source

public delegate void InvokeHandler();
public static void SafeInvoke(this System.Windows.Forms.Control control, InvokeHandler handler)
{
	if (control.InvokeRequired) control.Invoke(handler);
	else handler();
}

Example

//(from an event handler or public method on a form)

this.SafeInvoke(() =>
{
	textBox1.Text = "Hello, World!";
});

Author: David Harris

Submitted on: 21 aug. 2010

Language: C#

Type: System.Control

Views: 16491