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事件 }