task-parallel-library

Faulted vs Canceled task status after CancellationToken.ThrowIfCancellationRequested

北慕城南 提交于 2019-12-30 02:45:11
问题 Usually I don't post a question with the answer, but this time I'd like to attract some attention to what I think might be an obscure yet common issue. It was triggered by this question, since then I reviewed my own old code and found some of it was affected by this, too. The code below starts and awaits two tasks, task1 and task2 , which are almost identical. task1 is only different from task2 in that it runs a never-ending loop. IMO, both cases are quite typical for some real-life scenarios

Getting return value from Task.Run

淺唱寂寞╮ 提交于 2019-12-30 01:35:09
问题 I have the following code: public static async Task<string> Start(IProgress<ProcessTaskAsyncExProgress> progress) { const int total = 10; for (var i = 0; i <= total; i++) { await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))); if (progress != null) { var args = new ProcessTaskAsyncExProgress { ProgressPercentage = (int)(i / (double)total * 100.0), Text = "processing " + i }; progress.Report(args); } } return "Done"; } private static string RunLongTask(string taskName) {

How to measure performance of awaiting asynchronous operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 08:39:06
问题 I have a Windows Service that reads from multiple MessageQueue instances. Those messagequeues all run their own Task for reading messages. Normally, after reading a message, the work of an I/O database is done. I've found articles claiming it's a good idea to use async on I/O operations, because it would free up threads. I'm trying to simulate the performance boost of using async I/O opertations in a Console application. The Console application In my test environment, I have 10 queues.

How to better understand the code/statements from “Async - Handling multiple Exceptions” article?

我只是一个虾纸丫 提交于 2019-12-29 07:17:09
问题 Running the following C# console app class Program { static void Main(string[] args) { Tst(); Console.ReadLine(); } async static Task Tst() { try { await Task.Factory.StartNew (() => { Task.Factory.StartNew (() => { throw new NullReferenceException(); } , TaskCreationOptions.AttachedToParent ); Task.Factory.StartNew ( () => { throw new ArgumentException(); } ,TaskCreationOptions.AttachedToParent ); } ); } catch (AggregateException ex) { // this catch will never be target Console.WriteLine("**

Throw Exception inside a Task - “await” vs Wait()

℡╲_俬逩灬. 提交于 2019-12-29 06:41:11
问题 static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting on the task, an AggregateException is thrown. } try { t.Start(); await t; } catch (Exception e) { // When awating on the task, the exception itself is thrown. // in this case a regular Exception. } } In TPL, When throwing an exception inside a Task, it's wrapped with an AggregateException. But the same is not happening when

Throw Exception inside a Task - “await” vs Wait()

南楼画角 提交于 2019-12-29 06:41:02
问题 static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting on the task, an AggregateException is thrown. } try { t.Start(); await t; } catch (Exception e) { // When awating on the task, the exception itself is thrown. // in this case a regular Exception. } } In TPL, When throwing an exception inside a Task, it's wrapped with an AggregateException. But the same is not happening when

Plinq statement gets deadlocked inside static constructor

早过忘川 提交于 2019-12-29 05:30:07
问题 I came across this situation where the following plinq statement inside static constructor gets deadlocked: static void Main(string[] args) { new Blah(); } class Blah { static Blah() { Enumerable.Range(1, 10000) .AsParallel() .Select(n => n * 3) .ToList(); } } It happens only when a constructor is static. Can someone explain this to me please. Is it TPL bug? Compiler? Me? 回答1: It is generally dangerous to call threading code from a static constructor. In order to ensure that the static

How can I free-up memory used by a Parallel.Task?

孤人 提交于 2019-12-29 05:24:05
问题 I have a program that does a memory intensive simulation. Below I've written a small console application that replicates the problem I'm having. class Program { static void Main(string[] args) { var t = new Task(() => DoMemoryHog(20000000)); t.Start(); t.Wait(); t.Dispose(); t = null; GC.Collect(); Console.WriteLine("Done"); Console.ReadLine(); } static void DoMemoryHog(int n) { ConcurrentBag<double> results = new ConcurrentBag<double>(); Parallel.For(0, n, (i) => { results.Add(Math.Sqrt(i

Pipelines, multiplexing, and unbounded buffering

試著忘記壹切 提交于 2019-12-28 16:05:06
问题 (NOTE: I'm using .Net 4, not .Net 4.5, so I cannot use the TPL's DataflowBlock classes.) TL;DR Version Ultimately, I'm just looking for a way to process sequential work items using multiple threads in a way that preserves their order in the final output, without requiring an unbounded output buffer. Motivation I have existing code to provide a multithreaded mechanism for processing multiple blocks of data where one I/O-bound thread (the "supplier") is reponsible for enqueuing blocks of data

Why *not* change the priority of a ThreadPool (or Task) thread?

你说的曾经没有我的故事 提交于 2019-12-28 06:28:09
问题 There are many places across the web and Stack Overflow where one is discouraged from changing the priority of a ThreadPool thread or TPL Task . In particular: "You have no control over the state and priority of a thread pool thread." "The runtime manages the thread pool. You have no control over the scheduling of the thread, nor can you change the thread's priority." "You should not change the Culture or Priority or... of a PoolThread. Just like you don't paint or re-decorate a rental car."