Tuesday, February 23, 2010

Control.Invoke using Lambdas

Instead of using anonymous methods or delegates, you can now use a simple lambda expression. The default Control.Invoke() wont work so the best thing to do is write your own extension method. An example follows:

Extension Method


public static class ControlExtensions

{

    public static void Invoke(this Control Control, Action Action)

    {

        Control.Invoke(Action);

    }

}



Usage


this.Invoke(() => Text = "Refreshed");