MethodInvoker
Delegate
public delegate void
MethodInvoker()
Remarks
MethodInvoker provides a
simple delegate that is used to invoke a method with a void parameter
list. This delegate can be used when making calls to a control's
Invoke method, or when you need a simple delegate but do not want to
define one yourself.
Examples
The following code
example demonstrates how to use a MethodInvoker to call a method that
updates the title bar of the application form.
C#
public
partial class Form1 : Form
{
public Form1()
{
// Create a timer that will call the ShowTime method every second.
var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);
}
private void ShowTime(object x)
{
// Don't do anything if the form's handle hasn't been created
// or the form has been disposed.
if (!this.IsHandleCreated && !this.IsDisposed) return;
// Invoke an anonymous method on the thread of the form.
this.Invoke((MethodInvoker) delegate
{
// Show the current time in the form's title bar.
this.Text = DateTime.Now.ToLongTimeString();
});
}
}
Comments
Post a Comment