task-parallel-library


OperationCanceledException VS TaskCanceledException when task is canceled

核能气质少年 提交于 2020-02-19 09:35:33
问题 The following code creates a task which is being canceled. await expression (case 1) throws System.OperationCanceledException while synchronous Wait() (case 2) throws System.Threading.Tasks.TaskCanceledException (wrapped in System.AggregateException ). using System; using System.Threading; using System.Threading.Tasks; public class Program { public static void Main() { Program.MainAsync().Wait(); } private static async Task MainAsync() { using(var cancellationTokenSource = new

How to throttle multiple asynchronous tasks?

旧街凉风 提交于 2020-02-10 03:18:16
问题 I have some code of the following form: static async Task DoSomething(int n) { ... } static void RunThreads(int totalThreads, int throttle) { var tasks = new List<Task>(); for (var n = 0; n < totalThreads; n++) { var task = DoSomething(n); tasks.Add(task); } Task.WhenAll(tasks).Wait(); // all threads must complete } Trouble is, if I don't throttle the threads, things start falling apart. Now, I want to launch a maximum of throttle threads, and only start the new thread when an old one is

How to throttle multiple asynchronous tasks?

时光怂恿深爱的人放手 提交于 2020-02-10 03:18:08
问题 I have some code of the following form: static async Task DoSomething(int n) { ... } static void RunThreads(int totalThreads, int throttle) { var tasks = new List<Task>(); for (var n = 0; n < totalThreads; n++) { var task = DoSomething(n); tasks.Add(task); } Task.WhenAll(tasks).Wait(); // all threads must complete } Trouble is, if I don't throttle the threads, things start falling apart. Now, I want to launch a maximum of throttle threads, and only start the new thread when an old one is

Chaining Tasks in csharp with success and fault handler

泪湿孤枕 提交于 2020-02-05 03:31:09
问题 Edit See the title "Problem" at the end within my question to crack this question down. Coming from nodejs where we could chain promises, in C# I'm seeing Async Tasks almost comparable. Here's my attempt. Edit - I can't mark my uber level caller methods as async as a dll based library is calling it Caller object public void DoSomething(MyRequest request) { Delegate.Job1(request) .ContinueWith(Delegate.Job2) .ContinueWith(Fault, TaskContinuationOptions.OnlyOnFaulted) .ContinueWith(Result); }

Enable Async TransactionScope without TransactionScopeAsyncFlowOption.Enabled

╄→гoц情女王★ 提交于 2020-02-04 04:39:14
问题 Following is Async Cache and Database update using Transaction Scope. I cannot use TransactionScopeAsyncFlowOption.Enabled introduced in the v 4.5.1, since the Apache Ignite.Net Cache I am using doesn't support it. I have tried finding a workaround by capturing the current Synchronization Context and then explicitly using Synchronization Context Send method to complete the transaction, but this doesn't work as I still get an error Transaction scope must be disposed on same thread it was

Windows Service running Async code not waiting on work to complete

五迷三道 提交于 2020-01-31 08:40:27
问题 In Brief I have a Windows Service that executes several jobs as async Tasks in parallel. However, when the OnStop is called, it seems that these are all immediately terminated instead of being allowed to stop in a more gracious manner. In more detail Each job represents an iteration of work, so having completed its work the job then needs to run again. To accomplish this, I am writing a proof-of-concept Windows Service that: runs each job as an awaited async TPL Task (these are all I/O bound

Capturing Exceptions on async operations

梦想与她 提交于 2020-01-29 11:50:10
问题 I'm reading up more about async here: http://msdn.microsoft.com/en-us/library/hh873173(v=vs.110).aspx Going through this example: Task<bool> [] recommendations = …; while(recommendations.Count > 0) { Task<bool> recommendation = await Task.WhenAny(recommendations); try { if (await recommendation) BuyStock(symbol); break; } catch(WebException exc) { recommendations.Remove(recommendation); } } I wonder, if I'm already performing await on Task.WhenAny why do I need to await again inside of the

Accessing values in Task.ContinueWith

时光总嘲笑我的痴心妄想 提交于 2020-01-25 11:59:21
问题 I am starting a task with the following code: var token = tokenSource.Token; var taskWithToken = new Task(() => new ProcessMyCommand(_unitOfWork, ..., batchRunId, token).Execute(), token); In my continue with, I need to know the batchRunId and possibly some other variables listed in the ... , however, it doesn't apepar that this is possible??? taskWithToken.ContinueWith(task => { if (!task.IsCanceled) return; //TODO: make sure no more subsequent runs happen //TODO: sync with source data } );

Combining dataflow results

不问归期 提交于 2020-01-24 22:15:08
问题 I am develop a Dataflow pipeline which reads a collection of files and, for each line in each file, performs a series of Dataflow blocks. After all steps have completed for each line in a file, I am wanting to execute further blocks on the file itself, but I don't know how this is possible. It is straightforward to split processing via a TransformManyBlock , but how can one then consolidate? I am used to Apache Camel's Splitter and Aggregator functionality - or there a fundamental disconnect

What is the opposite block of BatchBlock

孤者浪人 提交于 2020-01-24 19:00:33
问题 Given : A list of ids of emails What I'm doing : With the BatchBlock group ids and call a Transformblock for each block the simplified transformblock looks like this: var readBatch = new TransformBlock<List<int>, IEnumerable<Email>>(idList => { List<Email> mails = new List<Email>(); foreach(var id in idList) { mails.Add(new Email(id)); } return mails; }); Now my next TransformBlock is defined like this TransformBlock<Email,EMail> filterStep; What I search : So I need a block which allows me

工具导航Map