惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
F
Full Disclosure
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
J
Java Code Geeks
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Threatpost
S
SegmentFault 最新的问题
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
C
Cyber Attacks, Cyber Crime and Cyber Security
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
S
Schneier on Security
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
G
GRAHAM CLULEY
Latest news
Latest news
P
Privacy International News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
G
Google Developers Blog
L
LangChain Blog
MyScale Blog
MyScale Blog
Project Zero
Project Zero
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
N
News | PayPal Newsroom
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
SecWiki News
SecWiki News
T
Tor Project blog
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学

博客园 - 许明会

OCR图像识别技术-Asprise OCR 关于委托,事件和类的设计准则 JavaScript能干什么? C#泛型代理、泛型接口、泛型类型、泛型方法 Delegate, Method as Parameter. DES对称性加密 利用委托实现异步调用 通过Windows组策略限制证书组织流氓软件的安装运行 枚举\位域\结构综合实验 - 许明会 - 博客园 public static void Invoke (Action action) C#编写WIN32系统托盘程序 C#的互操作性:缓冲区、结构、指针 SQLServer异步调用,批量复制 Python体验(10)-图形界面之计算器 Python体验(09)-图形界面之Pannel和Sizer Python体验(08)-图形界面之工具栏和状态栏 Python体验(07)-图形界面之菜单 C#利用WIN32实现按键注册 Javascript猜数字游戏
异步编程,采用WorkgroupWorker,async和await关键字
许明会 · 2016-05-02 · via 博客园 - 许明会

金科玉律:不要在UI线程上执行耗时的操作;不要在除了UI线程之外的其他线程上访问UI控件!

NET1.1的BeginInvoke异步调用,需要准备3个方法:功能方法GetWebsiteLength,结果方法DownloadComplete,呼叫方法BeginInvoke!

但很不幸,在UI线程之外访问UI线程控件!调用失败。线程同步必须在线程所属进程的公共区域保留同步区,以此实现线程间的通讯。

AsyncDemo

二、C#2.0引入了BackgroundWorker,从而极大的简化了线程间通讯。

BackgroundWorker

三、C#4.0引入的async和await关键字,使得一切变得如此简单!

async和await关键字

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsyncDemo
{
    class MyForm:Form
    {
        Label lblInfo;
        Button btnCaculate;
        public MyForm()
        {
            lblInfo = new Label { Location = new System.Drawing.Point(10, 20), Text = "Length" };
            btnCaculate = new Button { Location = new System.Drawing.Point(10, 50), Text = "GetLength" };
            btnCaculate.Click += DisplayWebsiteLength;
            AutoSize = true;
            this.Controls.Add(lblInfo);
            this.Controls.Add(btnCaculate);
        }
    
        async void DisplayWebsiteLength(object sender,EventArgs e)
        {
            lblInfo.Text = "Fetching...";
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                //string text = await client.GetStringAsync("http://flaaash.cnblogs.com");
                //lblInfo.Text = text.Length.ToString();

                Task<string> task = client.GetStringAsync("http://www.sina.com");
                string contents = await task;
                lblInfo.Text = contents.Length.ToString();
            }
        }
        static void Main(string[] args)
        {
            Application.Run(new MyForm());

        }
    }
}

BackgroundWorker

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AsyncDemo
{
    class BackWorker : Form
    {
        Label lblInfo;
        Button btnCaculate;
        System.ComponentModel.BackgroundWorker worker;

        BackWorker()
        {
            lblInfo = new Label { Location = new System.Drawing.Point(10, 20), Text = "Length" };
            btnCaculate = new Button { Location = new System.Drawing.Point(10, 50), Text = "GetLength" };
            btnCaculate.Click += (o, e) => { worker.RunWorkerAsync(); };
            this.Controls.Add(lblInfo);
            this.Controls.Add(btnCaculate);

            worker = new System.ComponentModel.BackgroundWorker();
            worker.DoWork += (o, e) =>
            {
                System.Net.WebClient client = new System.Net.WebClient();
                string contents = client.DownloadString("http://www.sina.com");
                e.Result = contents.Length;
            };
            worker.RunWorkerCompleted += (o, e) => lblInfo.Text = e.Result.ToString();
        }

        public static void MainTest(string[] args)
        {
            Application.Run(new BackWorker());
        }
    }
}