大家好,又见面了,我是你们的朋友全栈君。
C#并行计算 Parallel.ForEach
-
-
/// <summary> /// 获取订单链接 /// </summary> public void GetOrders() { GetToken(); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = "https://merchant-api.jet.com/api/orders/{status}".Replace("{status}", "acknowledged"); item.Method = "get"; item.ResultCookieType = ResultCookieType.CookieCollection; item.ContentType = "application/json"; item.Header.Add("Authorization", string.Format("bearer {0}", Token.id_token)); item.Header.Add("Accept-Encoding", "gzip, deflate"); item.PostEncoding = System.Text.Encoding.UTF8; item.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E; Tablet PC 2.0; TCO_20150304085044)"; item.KeepAlive = false; item.Header.Add("Cache-Control", "no-cache"); HttpResult result = http.GetHtml(item); OrdersUrl UrlInfo=result.Html.Str2Entity<OrdersUrl>(); //创建一个订单任务并发集合 ConcurrentQueue<OrderInfo> Queue = new ConcurrentQueue<OrderInfo>(); //并行计算 数据分区 均匀分布到各个内核 Parallel.ForEach(Partitioner.Create<string>(UrlInfo.Urls,true),url=> { OrderInfo Order = GetOrderDetail(url); Queue.Enqueue(Order); }); Parallel.ForEach(Partitioner.Create<OrderInfo>(Queue.ToArray(),true),order=> { SaveOrder(order); }); }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161792.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...