TPL Dataflow, whats the functional difference between Post() and SendAsync()?

To see the difference, you need a situation where blocks will postpone their messages. In this case, Post will return false immediately, whereas SendAsync will return a Task that will be completed when the block decides what to do with the message. The Task will have a true result if the message is accepted, and … Read more

How do I get a return value from Task.WaitAll() in a console app?

You don’t get a return value from Task.WaitAll. You only use it to wait for completion of multiple tasks and then get the return value from the tasks themselves. var task1 = GetAsync(1); var task2 = GetAsync(2); Task.WaitAll(task1, task2); var result1 = task1.Result; var result2 = task2.Result; If you only have a single Task, just … Read more

can not await async lambda

In your lambda example, when you call task.Wait(), you are waiting on the new Task that you constructed, not the delay Task that it returns. To get your desired delay, you would need to also wait on the resulting Task: Task<Task> task = new Task<Task>(async () => { await Task.Delay(1000); }); task.Start(); task.Wait(); task.Result.Wait(); You … Read more

Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern

Instead of handcranking your models try using something like the Json2csharp.com website. Paste In an example JSON response, the fuller the better and then pull in the resultant generated classes. This, at least, takes away some moving parts, will get you the shape of the JSON in csharp giving the serialiser an easier time and … Read more

Task.Run with Parameter(s)?

private void RunAsync() { //Beware of closures. String is immutable. string param = “Hi”; Task.Run(() => MethodWithParameter(param)); } private void MethodWithParameter(string param) { //Do stuff } Edit Due to popular demand I must note that the Task launched will run in parallel with the calling thread. Assuming the default TaskScheduler this will use the .NET … Read more

The lack of non-capturing Task.Yield forces me to use Task.Run, why follow that?

You don’t have to put the Task.Run in DoWorkAsync. Consider this option: public async Task UIAction() { // UI Thread Log(“UIAction”); // start the CPU-bound work var cts = new CancellationTokenSource(5000); var workTask = Task.Run(() => DoWorkAsync(cts.Token)); // possibly await for some IO-bound work await Task.Delay(1000); Log(“after Task.Delay”); // finally, get the result of the … Read more

Is it possible to get successful results from a Task.WhenAll when one of the tasks fails? [duplicate]

Maybe public async Task<Task[]> RejectFailedFrom(params Task[] tasks) { try { await Task.WhenAll(tasks); } catch(Exception exception) { // Handle failed tasks maybe } return tasks.Where(task => task.Status == TaskStatus.RanToCompletion).ToArray(); } Usage var tasks = new[] { Task.FromResult(1), Task.FromException<int>(new ArgumentException(“fail1”)), Task.FromException<int>(new ArgumentException(“fail2”)) }; var succeed = await RejectFailedFrom(tasks); // [ tasks[0] ]

Where can I find a TPL dataflow version for 4.0?

I wrote Steve from the TPL dataflow team about this problem and he responded me with the following download link: http://download.microsoft.com/download/F/9/6/F967673D-58D6-4E3F-8CA9-11769A0A63B1/TPLDataflow.msi This is a CTP version, but the date matches the Nuget package with version number 4.0, so I think it’s the latest version that was compiled against .NET 4.0.