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

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
美团技术团队
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
A
About on SuperTechFans
Recent Announcements
Recent Announcements
D
Docker
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
腾讯CDC
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志

博客园 - 烟雨客

Aspose.PDF跨平台问题解决 在aspx中写c# 在动态生成的HTML中执行JS AWS 2020 Innovate所有视频 sp_executesql 可動態傳入傳出參數 the input stream is not valid binary formate.... when call remoting 用SSISl连接PostgrsSQL Ubuntu 16.04 TLS下安装dotnet异常处理 客户端不刷新 在公网上布署Web Api的时候,不能调用,返回404 在公网(internet)上建立website时不能用http访问 ClickOnce发布后不能安装 从JPG中获取缩略图 MaraDNS与DeadWood一起配置为本地机器提供小型化DNS服务 The requested page cannot be accessed because the related configuration data for the page is invalid 404 Not Find When using Owin with OAuth Socket服务端口长连接最多能支持多少? Json序列化 关于获得本机Mac Address的方法
await, anync
烟雨客 · 2015-04-07 · via 博客园 - 烟雨客
public Form1()
        {
            InitializeComponent();
        }

        // The following method runs asynchronously. The UI thread is not
        // blocked during the delay. You can move or resize the Form1 window 
        // while Task.Delay is running.
        public async Task<string> WaitAsynchronouslyAsync()
        {
            await Task.Delay(10000);
            MessageBox.Show("fi");
            return "Finished";
        }

        // The following method runs synchronously, despite the use of async.
        // You cannot move or resize the Form1 window while Thread.Sleep
        // is running because the UI thread is blocked.
        public async Task<string> WaitSynchronously()
        {
            // Add a using directive for System.Threading.
            Thread.Sleep(10000);
            return "Finished";
        }

        private async void button1_Click_1(object sender, EventArgs e)
        {
            string result = string.Empty;
            WaitAsynchronouslyAsync();

            // Call the method that runs asynchronously.
            //result = await WaitAsynchronouslyAsync();
            // Call the method that runs synchronously.
            // result = await WaitSynchronously ();

            // Display the result.
            textBox1.Text += result;
            MessageBox.Show("hi");
        }
上述代码得出至少两点:
1. await WaitAsynchronouslyAsync() 执行时要等待这个调用完成,才会执下后面的代码,但在这个调用没有完成前,其thread是可以被其它代码占用的,表现就是UI仍可以接收其它消息
2. WaitAsynchronouslyAsync()函数执行的线程与UI主线程是同一个,这也是为什么在这个函数中的messagebox可以显示,Thread.sleep可以阻止UI线程接收消息

posted @ 2015-04-07 17:55  烟雨客  阅读(297)  评论()    收藏  举报