task-parallel-library

Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

烈酒焚心 提交于 2019-12-31 12:31:26
问题 I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection<T> , ConcurrentQueue<T> & GetConsumingEnumerable while trying to create a simple pipeline. In a nutshell, adding entries to a default BlockingCollection<T> (which under the hood is relying on a ConcurrentQueue<T> ) from one thread, does not guarantee that they will be popped off the BlockingCollection<T> from another thread calling the GetConsumingEnumerable() Method. I've created a very simple

Parallel.Foreach loop, inconsistent behavior with explicit throw statement

那年仲夏 提交于 2019-12-31 02:43:10
问题 Created a simple program using Linqpad, where I am throwing an exception explicitly in the Parallel Foreach loop, which ideally shall be caught in the caller as Aggregate Exception , but when I explicitly throw the exception, it sometimes skip out few exceptions on random basis. I am not able to understand the behavior, anyone who can explain: void Main() { try { var intList = new List<int> {1,2,3,4,5,6}; Parallel.ForEach(intList, i => Test1(i)); } catch (AggregateException aggregateException

Only last task runs!

元气小坏坏 提交于 2019-12-30 22:50:12
问题 I am in desperate to find a solution to my problem. Following is the code which generates different task for each item in List<AccountContactView> . List<AccountContactViewModel> selectedDataList = DataList.Where( dataList => (bool) dataList.GetType() .GetProperty("IsChecked") .GetValue(dataList, new object[0]) == true ).ToList(); this.IsEnabled = false; Task validateMarked = Task.Factory.StartNew(() => { foreach (AccountContactViewModel viewModel in selectedDataList) { if (viewModel != null)

Task Parallel Library for directory traversal

 ̄綄美尐妖づ 提交于 2019-12-30 18:43:05
问题 I'd like to traverse a directory on my hard drive and search through all the files for a specific search string. This sounds like the perfect candidate for something that could (or should) be done in parallel since the IO is rather slow. Traditionally, I would write a recursive function to finds and processes all files in the current directory and then recurse into all the directories in that directory. I'm wondering how I can modify this to be more parallel. At first I simply modified:

Exception handling outside of Task

白昼怎懂夜的黑 提交于 2019-12-30 11:49:41
问题 Just noticed strange thing: to catch exception in caller from new Task, lambda MUST be marked as async!? Is it really necessary even if delegate has no await operators at all? try { //Task.Run(() => // exception is not caught! Task.Run(async () => // unnecessary async!?! { throw new Exception("Exception in Task"); }).Wait(); } catch (Exception ex) { res = ex.Message; } Why there is neccesary for async operator? All documentation i can find tells that delegate must not return Void and Task

How to avoid nested AggregateException when using Task.ContinueWith?

走远了吗. 提交于 2019-12-30 11:03:44
问题 I have an async method in a .NET 4.5 C# component: public async Task<T> GetResultAsync() { return PerformOperationAsync(); } If PerformOperationAsync throws an exception, then I can catch an AggregateException on a client side, unwrap it and get the original exception thrown. However, if I have slightly more complicated code: public async Task<T> GetResultAsync() { return PerformOperationAsync().ContinueWith(x => { var result = x.Result; return DoSomethingWithResult(result); },

How to avoid nested AggregateException when using Task.ContinueWith?

巧了我就是萌 提交于 2019-12-30 11:03:36
问题 I have an async method in a .NET 4.5 C# component: public async Task<T> GetResultAsync() { return PerformOperationAsync(); } If PerformOperationAsync throws an exception, then I can catch an AggregateException on a client side, unwrap it and get the original exception thrown. However, if I have slightly more complicated code: public async Task<T> GetResultAsync() { return PerformOperationAsync().ContinueWith(x => { var result = x.Result; return DoSomethingWithResult(result); },

How to use async/await with a library that uses an event-based asynchronous pattern?

≯℡__Kan透↙ 提交于 2019-12-30 10:32:34
问题 I use a library that has an asynchronous method called DoWork(...) that will raise a WorkDone event when the operation completes. I would like to write a method that calls this library, but instead of maintaining the same pattern I would like my method to be async so it can be called with await . In essence, what I am trying to do is: public async Task<Result> SomeMethod() { var result = new Task<Result>(); library.WorkDone += (data) => { result.Result = data; } library.DoWork(); return await

Task.WaitAll freezes app C#

半城伤御伤魂 提交于 2019-12-30 10:25:45
问题 I wanted to try out Threading.Task (C#) to run some work in parallel. In this simple example I have a form with progress bar and button. On click the RunParallel function is called. Without Task.WaitAll() it seems to run through fine. However, with the WaitAll statement the form shows and nothing happens. I dont understand what I am doing wrong in the setup below. Thanks in advance. public partial class MainWindow : Form { public delegate void BarDelegate(); public MainWindow() {

Task.WaitAll freezes app C#

穿精又带淫゛_ 提交于 2019-12-30 10:25:12
问题 I wanted to try out Threading.Task (C#) to run some work in parallel. In this simple example I have a form with progress bar and button. On click the RunParallel function is called. Without Task.WaitAll() it seems to run through fine. However, with the WaitAll statement the form shows and nothing happens. I dont understand what I am doing wrong in the setup below. Thanks in advance. public partial class MainWindow : Form { public delegate void BarDelegate(); public MainWindow() {