Avoiding InvokeRequired

秒速五厘米 2021-12-08 16:53 276阅读 0赞

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. 1 static class ControlExtensions
  2. 2 {
  3. 3 public static void InvokeOnOwnerThread<T>(this T control, Action<T> invoker) where T : Control
  4. 4 {
  5. 5 if (control.InvokeRequired)
  6. 6 control.Invoke(invoker, control);
  7. 7 else
  8. 8 invoker(control);
  9. 9 }
  10. 10 }

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

  1. private void UpdateTextBoxFromSomeThread(string message)
  2. {
  3. textBox1.InvokeOnOwnerThread((txtBox) => txtBox.Text = message);
  4. }

Would like to give all the credits to its author.

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

转载于:https://www.cnblogs.com/feishunji/p/3210549.html

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读