task-parallel-library

Starting tasks inside a loop: how to pass values that can be changed inside the loop? [duplicate]

不羁的心 提交于 2020-07-15 07:08:41
问题 This question already has answers here : Captured variable in a loop in C# (9 answers) Closed 12 days ago . I'm trying to use TPL inside a while loop and I need to pass to the task some values that then changes into the loop. For instance, here it is shown an example with an index that is incremented (necessarily after the line in which the task creation is requested): int index = 0; Task[] tasks; while(/*condition*/) { tasks[index] = Task.Factory.StartNew(() => DoJob(index)); index++; } But

Are a .NET Task thread's resources returned back to the pool temporarily if the thread is waiting on an async operation to finish?

这一生的挚爱 提交于 2020-07-15 06:41:05
问题 I have a TPL Task that does two things. First, it calls a web service. Second, it inserts some data into a database. I have up to 20 Tasks started at one time doing this same thing over and over again. All they do all day is call web services and insert data into a database. I'm fairly new to TPL in .NET. I've done some stuff with background worker processes and async web services. The web service call and the database insert are both blocking calls within the thread the Task is running in. I

Reading millions of small files with C#

半世苍凉 提交于 2020-07-09 08:54:56
问题 I have millions of log files which generating every day and I need to read all of them and put together as a single file to do some process on it in other app. I'm looking for the fastest way to do this. Currently I'm using Threads, Tasks and parallel like this: Parallel.For(0, files.Length, new ParallelOptions { MaxDegreeOfParallelism = 100 }, i => { ReadFiles(files[i]); }); void ReadFiles(string file) { try { var txt = File.ReadAllText(file); filesTxt.Add(tmp); } catch { } GlobalCls

How to keep track of faulted items in TPL pipeline in (thread)safe way

此生再无相见时 提交于 2020-07-09 05:43:02
问题 I am using TPL pipeline design together with Stephen Cleary's Try library In short it wraps value/exception and floats it down the pipeline. So even items that have thrown exceptions inside their processing methods, at the end when I await resultsBlock.Completion; have Status=RunToCompletion . So I need other way how to register faulted items. Here is small sample: var downloadBlock = new TransformBlock<int, Try<int>>(construct => Try.Create(() => { //SomeProcessingMethod(); return 1; }));

How to keep track of faulted items in TPL pipeline in (thread)safe way

好久不见. 提交于 2020-07-09 05:42:29
问题 I am using TPL pipeline design together with Stephen Cleary's Try library In short it wraps value/exception and floats it down the pipeline. So even items that have thrown exceptions inside their processing methods, at the end when I await resultsBlock.Completion; have Status=RunToCompletion . So I need other way how to register faulted items. Here is small sample: var downloadBlock = new TransformBlock<int, Try<int>>(construct => Try.Create(() => { //SomeProcessingMethod(); return 1; }));

How to keep track of faulted items in TPL pipeline in (thread)safe way

南笙酒味 提交于 2020-07-09 05:42:07
问题 I am using TPL pipeline design together with Stephen Cleary's Try library In short it wraps value/exception and floats it down the pipeline. So even items that have thrown exceptions inside their processing methods, at the end when I await resultsBlock.Completion; have Status=RunToCompletion . So I need other way how to register faulted items. Here is small sample: var downloadBlock = new TransformBlock<int, Try<int>>(construct => Try.Create(() => { //SomeProcessingMethod(); return 1; }));

TPL Dataflow: Why does EnsureOrdered = false destroy parallelism for this TransformManyBlock?

元气小坏坏 提交于 2020-06-27 13:17:15
问题 I'm working on a TPL Dataflow pipeline and noticed some strange behaviour related to ordering/parallelism in TransformManyBlock s (might apply to other blocks as well). Here is my code to reproduce (.NET 4.7.2, TPL Dataflow 4.9.0): class Program { static void Main(string[] args) { var sourceBlock = new TransformManyBlock<int, Tuple<int, int>>(i => Source(i), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4, EnsureOrdered = false }); var targetBlock = new ActionBlock<Tuple<int,

Understanding TPL Dataflow Degree of Parallelism ordering

这一生的挚爱 提交于 2020-06-27 08:49:10
问题 I was reading Dataflow (Task Parallel Library), and there is a portion which says: When you specify a maximum degree of parallelism that is larger than 1, multiple messages are processed simultaneously, and therefore, messages might not be processed in the order in which they are received. The order in which the messages are output from the block will, however, be correctly ordered. What does it means? Example, I set my action block with degree of parallelism = 5: testActionBlock = new

How to make ASP.NET MVC Controller Methods Async

六月ゝ 毕业季﹏ 提交于 2020-06-26 07:29:28
问题 I'm launching multiple ajax calls to various MVC controllers to load different parts of my page. However it seems that when this gets to the controller only one runs at a time. I'm guessing this is because by default ASP.Net MVC controllers are synchronous? I've also tested loading a page on 2 browser tabs and the second tab always waits for the first. To get round this I've attempted to make the controller methods in question asynchronous. I've done this by doing the following Append Async

How do I fix the deadlock on a threadpool thread that has a SynchronizationContext?

梦想的初衷 提交于 2020-06-24 12:34:07
问题 I am getting intermittent deadlocks when using HttpClient to send http requests and sometimes they are never returning back to await SendAsync in my code. I was able to figure out the thread handling the request internally in HttpClient / HttpClientHandler for some reason has a SynchronizationContext during the times it is deadlocking. I would like to figure out how the thread getting used ends up with a SynchronizationContext , when normally they don't have one. I would assume that whatever