C# 异步机制
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线程上执行的。
C# 异步机制的更多相关文章
- 结合源码看nginx-1.4.0之nginx异步机制详解
目录 0. 摘要 1. nginx异步设计思想 2. nginx异步设计数据结构 3. nginx异步机制源码解析 4. 一个简单的应用异步例子 5. 小结 6. 参考源码
- JavaScript的异步机制
我们经常说JS是单线程的,比如node.js研讨会上大家都说JS的特色之一是单线程的,这样使JS更简单明了,可是大家真的理解所谓JS的单线程机制吗?单线程时,基于事件的异步机制又该当如何 1 先看下两 ...
- JavaScript单线程和异步机制
随着对JavaScript学习的深入和实践经验的积累,一些原理和底层的东西也开始逐渐了解.早先也看过一些关于js单线程和事件循环的文章,不过当时看的似懂非懂,只留了一个大概的印象:浏览器中的js程序时 ...
- 深入理解Spring的异步机制
一.Spring中实现异步执行 在这里我先以事件的机制举例,注意默认情况下事件的发布与监听都是同步执行的.那么我们来看一看基于异步事件的例子该怎么写 首先还是定义事件: package com.bdq ...
- [转]JavaScript异步机制详解
原文: https://www.jianshu.com/p/4ea4ee713ead --------------------------------------------------------- ...
- Windows store app[Part 4]:深入WinRT的异步机制
接上篇Windows store app[Part 3]:认识WinRT的异步机制 WinRT异步机制回顾: IAsyncInfo接口:WinRT下异步功能的核心,该接口提供所有异步操作的基本功能,如 ...
- Windows store app[Part 3]:认识WinRT的异步机制
WinRT异步机制的诞生背景 当编写一个触控应用程序时,执行一个耗时函数,并通知UI更新,我们希望所有的交互过程都可以做出快速的反应.流畅的操作感变的十分重要. 在连接外部程序接口获取数据,操作本地数 ...
- 前端知识点回顾之重点篇——JavaScript异步机制
JavaScript异步机制 来源:https://www.cnblogs.com/zhaodongyu/p/3922961.html JavaScript是单线程异步执行的,单线程意味着代码在任务队 ...
- Js异步机制的实现
Js异步机制 JavaScript是一门单线程语言,所谓单线程,就是指一次只能完成一件任务,如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任务,以此类推.这种模式的好处是实现起来比较简单 ...
- JavaScript异步机制
单线程异步执行的JavaScript JavaScript是单线程异步执行的,单线程意味着代码在任务队列中会按照顺序一个接一个的执行.异步代表JavaScript代码在任务队列中的顺序并不完全等同于代 ...
随机推荐
- Day 21 内存处理与正则
一.python的垃圾回收机制 1.引用计数(垃圾回收机制的根本) 1.引用计数是用来记录值的内存地址被记录的次数的 2.每一次对值地址的引用都可以使该值的引用计数+1 3.每一次对值地址的释放都可以 ...
- Day 18 常用模块(二)
一.随机数:RANDOM 1.(0,1)小数:random.random() 2.[1,10]整数:random.randint(1,10) 3.[1,10)整数:random.randrang(1, ...
- SourceInsight宏插件2(非常好用,强力推荐)
Quicker宏在SI中的使用方法(下载地址:链接:https://pan.baidu.com/s/1VrDxlPhft7RPUCCOKxsGIg 提取码:2d4u) Quicker宏的添加到SI中 ...
- C# 控制CH341进行SPI,I2C读写
之前看了别人是C++ MFC开发的CH341 I2C,SPI读写flash的软件代码,看上去有点头晕,感觉还是使用C# winform开发更人性化些 C#控制需要调用CH341提供的DLL库,网上多数 ...
- RN 各种小问题
问题:vs code 无法启动 双击无反应 方法:控制台 输入 netsh winsock reset 重启 vs 问题:RN Debugger Network 不显示 源网页:https://git ...
- 工控随笔_07_西门子_WinCC利用命令行实现操作log日志
在WinCC中可以通过报警纪录来实现操作员纪录,这个需要WinCC的消息系统进行组态和配置. 利用消息系统进行实现上诉功能不但复杂而且时间久啦也不方便查询.那么有没有一种简单的方法来 实现操作员纪录呢 ...
- Docker基础操作
安装 ubuntu16.04安装docker sudo apt-get update sudo apt-get install docker sudo apt-get install docker.i ...
- IDEA 运行spring boot出现端口占用的问题
Description: The Tomcat connector configured to listen on port 8080 failed to start. The port may al ...
- Windows中的备份和还原
1:增量备份 (查看文件属性,只备打了勾的,备完后把勾取消掉,修改后勾都会打上) 2:差异备份(备份有勾的 ,备份完后不会取消勾) 周一:备一 周二:备二 周三:备三 周四:备三,四
- SAS 日期格式显示年月的format
首先要感谢bobguy在人大论坛上的帮助!之前和webgu也在圈子里讨论过这个问题,只找到一个yymmn6.的format,只能应用于yyyymm的情况.有了bobguy大侠的帮助,我们现在就可以表达 ...