Avoiding InvokeRequired

Just read a good article to do cross-thread calling in an easy and elegant way.

It is amazing for its simplicity and elegancy:

 1 static class ControlExtensions

 2 {

 3   public static void InvokeOnOwnerThread<T>(this T control, Action<T> invoker) where T : Control

 4   {

 5     if (control.InvokeRequired)

 6       control.Invoke(invoker, control);

 7     else

 8       invoker(control);

 9   }

10 } 

Only 10 lines to make the further calling a single line and extremely easy to read, very beautiful the way I like.

private void UpdateTextBoxFromSomeThread(string message)

{

  textBox1.InvokeOnOwnerThread((txtBox) => txtBox.Text = message);  

}

Would like to give all the credits to its author.

Original link: http://www.codeproject.com/Tips/374741/Avoiding-InvokeRequired

你可能感兴趣的:(require)