Tag: 并行处理

如何在Windows Phone上运行并行任务?

我正在构建一个WP8应用程序,我需要执行大约30个Web请求。 这些请求不依赖于彼此,因此可以并行化。 我的代码看起来像这样(简化/伪代码): foreach (Uri uri in uris) { var rawData = await Task.Run(() => httpClient.GetStringAsync(uri).ConfigureAwait(false)); var processedData = dataProcessor.Process(rawData); processedDataCollection.Add(processedData); } 当我看到Fiddler时,请求全部都是sequantial; 它需要几秒钟来执行和处理所有这些。 但是,我不希望代码等到1个请求完成后才转移到下一个请求,我想同时执行多个请求。 通常我会使用Parallel.Invoke()或Parallel.ForEach()或类似的东西来执行此操作,但显然并行库在Windows Phone 8中不可用。 那么实现这一目标的最佳方法是什么? Task.Factory.StartNew() ? new Thread() ?

在先前的异步操作完成之前,在该上下文上开始第二个操作

信息: “System.NotSupportedException was unhandled Message: An unhandled exception of type ‘System.NotSupportedException’ occurred in mscorlib.dll Additional information: A second operation started on this context before a previous asynchronous operation completed. Use ‘await’ to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread […]

CancellationToken取消不会破坏BlockingCollection

我有这样的取消令牌 static CancellationTokenSource TokenSource= new CancellationTokenSource(); 我有一个像这样的阻止集合 BlockingCollection items= new BlockingCollection(); var item = items.Take(TokenSource.Token); if(TokenSource.CancelPending) return; 我打电话的时候 TokenSource.Cancel(); Take不会像它应该的那样继续下去。 如果我使用带轮询的TryTake,令牌显示它被设置为已取消。

如何使用Parallel.For / ForEach获得最大性能? (包括性能时间)

我正在尝试并行化我的Web解析工具,但速度增益似乎非常小。 我有i7-2600K(8核超线程)。 这里有一些代码可以向您展示这个想法。 我只展示了Parallel.ForEach但你明白了: List AllLinks = this.GetAllLinks(); ConcurrentDictionary AllTopics = new ConcurrentDictionary ( ); int count = 0; Stopwatch sw = new Stopwatch ( ); sw.Start ( ); Parallel.ForEach ( AllLinks, currentLink => { Topic topic = this.ExtractTopicData ( currentLink ); this.AllTopics.TryAdd ( currentLink, topic ); ++count; if ( count > 50 ) { […]

如何在某些任务非常昂贵的任务中加载平衡并行性?

我有一个需要处理的对象列表。 所以说列表是所有客户的列表,我需要对所有客户执行CPU密集型计算。 虽然在此计算之前和之后我需要将数据提取并提交回数据库,因此它不仅仅是一个CPU任务。 所以我做的是 Parallel.ForEach(list, action); 行动在字面上 1 Fetch customer data 2 Process calculate (time and memory intensive task) 3 Commit back customer data 代码过去运行良好,但最近有时当处理了大量记录数量较多的客户时,系统内存不足。 那么有没有办法对此进行负载均衡? 大多数客户都得到了快速处理,但很少有人将所有资源都拿走了。 我可以避免其中一些一起跑吗? 我可以实现这一点的一种方法是根据大小对列表进行排序,然后尝试选择第一个和最后一个项目并自己控制并行性,但是想看看我在这里有哪些选项。

如何实现multithreading并行执行多个任务?

我是线程编程的新手。 我必须在PARALLEL和Background中运行一些任务(以便主UI执行线程保持对用户操作的响应)并等待每个任务完成,然后再继续执行。 就像是: foreach(MyTask t in myTasks) { t.DoSomethinginBackground(); // There could be n number of task, to save // processing time I wish to run each of them // in parallel } // Wait till all tasks complete doing something parallel in background Console.Write(“All tasks Completed. Now we can do further processing”); 我知道可以有几种方法来实现这一目标。 但我正在寻找在.Net […]

SQL Server CLR UDF Parallelism redux

我一直在研究SQL Server CLR UDF和并行性。 普遍的共识似乎是在SQL Server 2008及更高版本中,带有DataAccessKind.None的标量值CLR UDF应该允许并行执行。 但是,当我在SQL Server 2012的视图中使用我的标量值UDF时,它仍然会杀死连接中的并行执行等。 有什么特别的东西需要添加到我的C#代码或T-SQL UDF定义中,以表明它对于并行执行是安全的吗? 谢谢。

如何制作IEnumerable方法的并行方法

在这篇文章之后 ,我想要并行化这个方法: public IEnumerable GetAllLogs(IEnumerable computers) { foreach (var cpt in computers) { foreach (var log in cpt.GetLogs()) { yield return log; } } } 当其中一个方法GetLogs完成时,我希望方法“yield returns”成为日志。 如果我有4台计算机返回: 计算机01:“a”,“b”,“c”,“d”,“e” 计算机02:“1”,“2”,“3”,“4”,“5” 计算机03:“alpha”,“beta”,“gamma”,“delta”,“epsilon” 计算机04:“我”,“II”,“III”,“IV”,“V” 使用“顺序方法”,输出为: a b c d e 1 2 3 4 5 alpha beta gamma delta epsilon I II III IV V 这些方法在20秒内运行。 GetLogs方法中有一个Thread.Sleep(1000) […]

在这种情况下,为什么使用AsParallel()比foreach慢?

我正在从这种格式的excel中提取数据 product1 | unnamedcol2 | product2 | unnamedcol4 | product3 | unnamedcol6 | ————————————————– —————————– @ 1foo | 1.10 | @ 1foo | 0.3 | @ 1foo | 0.3 @ 2foo | 1.00 | @ 2foo | 2 | @ 2foo | @ 3foo | 1.52 | @ 3foo | 2.53 | @ 3foo | […]

C#中的并行事件处理

我正在开发一个模块,它必须处理来自外部系统的许多事件。 我要使用提供事件(OnNewMessage)的第三方类将一些参数作为输入传递,将两个参数作为输出传递,每个事件需要一堆时间才能被处理。 我想以并行方式提供这些事件,以避免阻塞调用者并并行处理多个请求。 这是我的代码示例: void Init() { provider.OnNewMessage += new OnMessageEventHandler(processEvent); } void processEvent(string xml, int …, out string resultXML, out string description) { … } 在C#3.5中执行此操作的最佳方法是哪种? 谢谢