• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#异步调用(异步模式,基于事件)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
c# 异步调用之异步模式
Func<string, IImageRequest, string> downloadString = (address, req) => { var client = new WebClient(); foreach (var header in req.Headers) { client.Headers.Add(header.Key, header.Value); } client.Credentials = req.Credentials; client.Encoding = System.Text.Encoding.UTF8; return client.DownloadString(address); }; Action<SearchItemResult> addItem = item => _searchInfo.List.Add(item); foreach (var req in GetSearchRequests()) { downloadString.BeginInvoke(req.Url, req, ar =>//BeginInvoke:线程池中一个线程异步调用 { try { string resp = downloadString.EndInvoke(ar);//downloadString异步方法执行结束,执行ar委托 var images = req.Parse(resp); foreach (var image in images) { this.Dispatcher.Invoke(addItem, image);//当前线程回到主线程同步执行 } } catch (WebException ex) when (ex.Message.Contains("401")) { MessageBox.Show(errorMessage, "Registration Needed"); } }, null); }

  

c#异步调用之基于事件
foreach (var req in GetSearchRequests()) { var client = new WebClient(); foreach (var header in req.Headers) { client.Headers.Add(header.Key, header.Value); } client.Credentials = req.Credentials; client.DownloadStringCompleted += (sender1, e1) => { try { string resp = e1.Result; var images = req.Parse(resp); foreach (var image in images) { _searchInfo.List.Add(image);//事件处理程序可以直接访问UI } } catch (Exception ex) when (ex.InnerException?.Message.Contains("401") ?? false) { MessageBox.Show(errorMessage, "Registration Needed"); } }; client.DownloadStringAsync(new Uri(req.Url));//异步变体方法 以“async”后缀;结束之后会调用事件,此方法对应DownloadStringCompleted事件 }

  

//异步调用之基于任务(TAP)
        private async void OnTaskBasedAsyncPattern(object sender, RoutedEventArgs e)//async关键字创建了一个状态机
        {
            _cts = new CancellationTokenSource();
            try
            {
                foreach (var req in GetSearchRequests())
                {
                    var clientHandler = new HttpClientHandler
                    {
                        Credentials = req.Credentials
                    };
                    var client = new HttpClient(clientHandler);
                    foreach (var header in req.Headers)
                    {
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }

                    var response = await client.GetAsync(req.Url, _cts.Token);//解除线程阻塞
                    response.EnsureSuccessStatusCode();
                    string resp = await response.Content.ReadAsStringAsync();//解除线程阻塞
                    await Task.Run(() =>//后台线程运行
                    {
                        var images = req.Parse(resp);
                        foreach (var image in images)
                        {
                            _cts.Token.ThrowIfCancellationRequested();
                            _searchInfo.List.Add(image);//UI调用问题:BindingOperations.EnableCollectionSynchronization,启用集合同步访问功能
                        }
                    }, _cts.Token);
                }
            }
            catch (HttpRequestException ex) when (ex.Message.Contains("401"))
            {
                MessageBox.Show(errorMessage, "Registration Needed");
            }
            catch (OperationCanceledException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
从零开始编写自己的C#框架----系列文章发布时间:2022-07-13
下一篇:
C#多线程编程(4):多线程与UI操作发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap