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

推荐订阅源

J
Java Code Geeks
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 烟雨客

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)  评论()    收藏  举报