zoukankan      html  css  js  c++  java
  • 控件的Invoke()、BeginInvoke()与委托的Invoke()、BeginInvoke()

    Delegate.Invoke

    Delegate.Invoke is used to execute a delegate on the current thread. A delegate is just a reference to a function or method, and Delegate.Invoke is the mechanism to call this function or method (similar to a normal function call).

    Delegate.BeginInvoke

    Delegate.BeginInvoke is used to execute a delegate asynchronously, that is, on a separate thread. That means that you can start an operation which won't block your current thread, it will be executed on it's own thread. Delegate.BeginInvoke the operation will be performed "in the background", that is, on a separate thread. Note that you need to call EndInvoke at some time after BeginInvoke to avoid resource leaks.

    As its a seperate thread you must not update any property or call any method of a Windows Forms user that potentially update the UI. If you want to update a progress bar, fill a list, or do somehting similar while the operation is performed, you have to use a different mechanism: Control.Invoke or Control.BeginInvoke.

    Control.Invoke

    Control.Invoke is used to execute a delegate on the UI thread of that control. If you have a delegate which updates the user interface, you can call Control.Invoke from your other thread to execute the update operation on the UI thread.

    Control.BeginInvoke

    Control.BeginInvoke does the same, but in an asynchronous way. This means that, while Control.Invoke waits until the UI thread has finished executing the delegate, Control.BeginInvoke returns immediately.

    Notes to remember:

    Control.Invoke, Control.BeginInvoke, and Control.InvokeRequired are exceptions and can use these from other threads than the UI thread. 

    总的来说就是Delegate.BeginInvoke()是从线程池中取出一个新线程来执行,Delegate.Invoke()和普通调用效果上没什么区别。

    而Control.BeginInvoke()和Control.Invoke()都是返回到UI线程上执行的。

  • 相关阅读:
    STM32时钟配置方法详解
    STM32 入门之 GPIO
    arm可以干什么
    四两拨千斤,ARM是如何运作、靠什么赚钱的
    ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
    ARM与单片机到底有啥区别
    实得打印机色带芯更换
    IE浏览器不能上传图片
    IE FANS
    win8,win10里面内置的IE浏览器网银无法输入密码
  • 原文地址:https://www.cnblogs.com/zxhoo/p/1983634.html
Copyright © 2011-2022 走看看