【探究】C#控制台程序和WinForms程序对async方法的处理差异
acmilan2016/04/09软件综合 IP:四川
我相信很多人在学习C# 5.0的async和await关键字时有困惑的。为什么在GUI或XXXXXXT中不能对返回的Task进行Wait()或Result,而必须用await?为什么必须async到底?为什么在控制台程序中又可以进行Wait()或Result?为了解决这些问题,我们必须理解它的工作方式。

1.调用async方法,系统的行为是什么?

直接调用async方法,和调用普通方法没什么区别,只不过在被调用者第一次await的时候就返回了,而不是像通常那样,等待被调用者执行完才返回。

2.对于控制台程序,在方法内await子方法,系统的行为是什么?

将父方法注册为子方法的线程回调 -> 休眠父方法 -> 返回或休眠线程
子方法return -> 新建线程或唤醒线程 -> 继续执行父方法

3.对于GUI程序或XXXXXXT程序,在方法内await子方法,系统的行为是什么?

将父方法注册为子方法的消息回调 -> 休眠父方法 -> 返回或退入消息循环
子方法return -> 将父方法加入消息队列 -> 退入消息循环 -> 父方法被消息循环调度 -> 继续执行父方法

4.如何让方法在控制台和在GUI程序表现一致?

对子方法返回的Task对象调用.ConfigureAwait(false),再进行await,返回时就是在新线程中运行,而不是在主线程中运行了。注意GUI界面操作必须由主线程进行,也就是说,在await之后不可以进行任何的GUI界面操作,否则会引起异常。

【实验1】控制台程序的async/await特性

实验源码:
<code class="lang-c">using System;
using System.Threading;
using System.Threading.Tasks;
                      
namespace console1
{
    class Program
    {
        static async Task Async()
        {
            Console.WriteLine("Async start thread: " + Thread.CurrentThread.ManagedThreadId);
            await Task.Delay(1); // 交出控制权1ms
            Console.WriteLine("Async awaited thread: " + Thread.CurrentThread.ManagedThreadId);
            for (int i = 0; i < 50; i++)
            {
                Console.Write("b" + Thread.CurrentThread.ManagedThreadId + " ");
                Thread.Sleep(1);
            }
        }
                              
        static async Task AsyncMain()
        {
            Task t = Async(); // 调用另一个异步函数Async(),第一次await时返回
            Console.WriteLine("AsyncMain start thread: " + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(1); // 线程休眠1ms
            Console.WriteLine("AsyncMain slept thread: " + Thread.CurrentThread.ManagedThreadId);
            for (int i = 0; i < 50; i++)
            {
                Console.Write("a" + Thread.CurrentThread.ManagedThreadId + " ");
                Thread.Sleep(1);
            }
            await t; // 交出控制权,直到Async()运行完毕
        }
                              
        public static void Main(string[] args)
        {
            Console.WriteLine("main thread: " + Thread.CurrentThread.ManagedThreadId);
            AsyncMain().Wait(); // 由于Main不能被标记为async,需要用这种方式调用
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}</code>

实验结果:

Capture2.png

【实验2】WinForms程序的async/await特性

实验源码(插入两个控件,button1和textBox1,button1的Click事件注册为Button1Click,textBox1设置MultiLine为True,设置ScrollBars为Vertical):
<code class="lang-c">using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
                      
namespace sssabc
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            textBox1.Text += "main thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n"; 
        }
                              
        async Task Async()
        {
            textBox1.Text += "Async start thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n";
            await Task.Delay(1); // 交出控制权1ms
            textBox1.Text += "Async awaited thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n";
            for (int i = 0; i < 50; i++)
            {
                textBox1.Text += "b" + Thread.CurrentThread.ManagedThreadId + " ";
                Thread.Sleep(1);
            }
        }
                              
        async void Button1Click(object sender, EventArgs e)
        {
            Task t = Async(); // 调用另一个异步函数Async(),第一次await时返回
            textBox1.Text += "Button1Click start thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n";
            Thread.Sleep(1); // 线程休眠1ms
            textBox1.Text += "Button1Click slept thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n";
            for (int i = 0; i < 50; i++)
            {
                textBox1.Text += "a" + Thread.CurrentThread.ManagedThreadId + " ";
                Thread.Sleep(1);
            }
            await t; // 交出控制权,直到Async()运行完毕
        }
    }
}</code>

实验结果:

Capture.png

【实验3】在WinForms中实现并行运行的async方法

实验源码:
<code class="lang-c">using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
                  
namespace sssabc
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private StringBuilder outtxt = new StringBuilder();
                          
        public MainForm()
        {
            InitializeComponent();
            outtxt.Append("main thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n");
        }
                          
        async Task Async()
        {
            outtxt.Append("Async start thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n");
            await Task.Delay(1).ConfigureAwait(false); // 交出控制权1ms,返回时在新线程执行
            outtxt.Append("Async awaited thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n");
            for (int i = 0; i < 50; i++)
            {
                outtxt.Append("b" + Thread.CurrentThread.ManagedThreadId + " ");
                Thread.Sleep(1);
            }
        }
                          
        async void Button1Click(object sender, EventArgs e)
        {
            Task t = Async(); // 调用另一个异步函数Async(),第一次await时返回
            outtxt.Append("Button1Click start thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n");
            Thread.Sleep(1); // 线程休眠1ms
            outtxt.Append("Button1Click slept thread:" + Thread.CurrentThread.ManagedThreadId + "\r\n");
            for (int i = 0; i < 50; i++)
            {
                outtxt.Append("a" + Thread.CurrentThread.ManagedThreadId + " ");
                Thread.Sleep(1);
            }
            await t; // 交出控制权,直到Async()运行完毕
            textBox1.Text = outtxt.ToString();
        }
    }
}</code>

实验结果:

Capture3.png

【实验结论】

在控制台程序中,异步方法的唤醒是通过创建线程实现的,是在新线程中唤醒的,是并行化的。
在WinForms程序中,异步方法的唤醒是通过消息循环实现的,是在主线程中唤醒的,是串行化的。
在WinForms程序中,可以通过.ConfigureAwait(false)实现并行化,await之后的代码在新线程运行,不能进行任何GUI界面操作。

[修改于 8年2个月前 - 2016/04/09 20:14:55]

来自:计算机科学 / 软件综合
2
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
8年1个月前 修改于 8年1个月前 IP:四川
816424
async/await的实质

其实.NET 4.5的async和await就是一个语法糖而已,完全可以使用XXXXXXXntinueWith(t => { ... });实现,这样用lambda表达式的闭包特性即可实现异步编程。

async和await存在的必要性还是有的。比如说可以在循环内await,非常方便。而使用闭包进行异步编程的话,作用域会被分割,对于顺序执行和条件执行还好说,对于循环,编写起来是比较困难的(当然也并不是没有办法)。await解决了这些问题。
<code class="lang-c">using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
                     
namespace mywinforms1234
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
                                 
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        async Task<int> asyncmethod()
        {
            await Task.Delay(1000);
            return 1;
        }
        void Button1Click(object sender, EventArgs e)
        {
            Task<int> a = asyncmethod();
            a.ContinueWith(t => {
                MessageBox.Show(t.Result.ToString());
            });
        }
    }
}</int></int></code>

如果用纯粹的lambda代码实现asyncmethod,应该是这样的:
<code class="lang-c">Task<int> asyncmethod()
{
    return Task.Delay(1000).ContinueWith(t => {
        return 1;
    });
}</int></code>

和await不同的是,无论是GUI程序还是控制台程序,ContinueWith都是在新线程中继续执行的。在GUI程序中,要想达到像await一样的效果(插入到主线程的消息循环中继续执行),需要在第二个参数加上XXXXXXXXXXXXXXXXomCurrentSynchronizationContext(),这样的话操作GUI部件便不会报错。
<code class="lang-c">void Button1Click(object sender, EventArgs e)
{
    Task<int> a = asyncmethod();
    a.ContinueWith(t => {
        MessageBox.Show(t.Result.ToString() + " " + Thread.CurrentThread.ManagedThreadId);
        Text = "finished";
    }, TaskScheduler.FromCurrentSynchronizationContext());
}</int></code>
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
acmilan作者
8年1个月前 修改于 8年1个月前 IP:四川
816453
对于异步编程模式的应用建议

如果需要便于维护的异步编程,使用await。如果希望使用传统的闭包式异步编程,或者需要编写兼容.NET4.0的程序,使用ContinueWith。

如果需要在异步编程中操作GUI,应使用主线程继续执行。如果不需要操作GUI,则应该尽量避免占用主线程,以避免性能下降和死锁。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年3个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}