task-parallel-library

Parallel.Foreach loop creating multiple db connections throws connection errors?

好久不见. 提交于 2019-12-24 01:00:00
问题 The 'login failed' error has never resurfaced, curious? However the other connection errors seem to be explained by slamming the server. The randomness with which the errors occur, still a mystery. Login failed. The login is from an untrusted domain and cannot be used with Windows authentication I wanted to get my code running in parallel, so I changed my foreach loop to a parallel foreach loop. It seemed simple enough. Each loop connects to the database, looks up some stuff, performs some

Calling hundreds of azure functions in parallel

那年仲夏 提交于 2019-12-23 22:19:42
问题 I have an application that executes rules using some rules engine. I have around 500+ rules and our application will receive around 10,000 entries and all of those 10,000 entries should go through those 500 rules for validation individually. We are currently planning to migrate all our rules into Azure functions. That is each Azure function will correspond to a single rule. So I was trying a proof of concept and created couple of Azure functions with Task.delay to mock real rule validation. I

Is there a TPL dataflow block that doesn't take input but returns output?

半城伤御伤魂 提交于 2019-12-23 22:01:31
问题 The title of my question says it all. I am looking for a TPL dataflow block that doesn't need an input. Right now I am using a transform block but it's input is unused. 回答1: I would build a block like this from a BufferBlock<T> : the method accepts a delegate that presents the ITargetBlock<T> side of the block and returns the ISourceBlock<T> side of it. This way, the delegate can send input to the block, but from the outside, it looks like a block that only produces output. The code: public

TPL-like library for .NET 2.0

我怕爱的太早我们不能终老 提交于 2019-12-23 20:15:30
问题 After discovering the Parallels library in .NET 4.0, I'd like to have something similar in my .NET 2.0 application. Are there any parallel programming helper libraries for .NET 2.0, that simplify common tasks? 回答1: Threadpool would be a nomination in .NET 2.0 which is kind of similar with TPL. Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers. TPL actually uses Threadpool under the hood:

Why C# Parallel.Invoke is slow?

て烟熏妆下的殇ゞ 提交于 2019-12-23 20:14:40
问题 I am doing this: private static void Main(string[] args) { var dict1 = new Dictionary<int, string>(); var dict2 = new Dictionary<int, string>(); DateTime t1 = DateTime.Now; for (int i = 1; i < 1000000; i++) { Parallel.Invoke( () => dict1.Add(i, "Test" + i), () => dict2.Add(i, "Test" + i) ); } TimeSpan t2 = DateTime.Now.Subtract(t1); Console.WriteLine(t2.TotalMilliseconds); Console.ReadLine(); } So doing a for loop 1 million time and adding items to two different dictionaries. The problem is

What is the difference between these two functions using async/await/TPL?

徘徊边缘 提交于 2019-12-23 19:42:59
问题 I think I have successfully confused myself for the day. public void DoSomething1() { Task.Delay(1000); } public async void DoSomething2() { await Task.Delay(1000); } What is the difference between these two functions in terms of what happens within them when they are called? What is the purpose of using an async method that does not return a Task ? 回答1: What is the difference between these two functions in terms of what happens within them when they are called? DoSomething1 is a synchronous

Detect errors with NetworkStream.WriteAsync

拥有回忆 提交于 2019-12-23 19:03:14
问题 If I kill my server after the call to Login is complete, no exception is thrown when the call to stream.WriteAsync(data, 0, data.Count()); is made and there is no indication of error in the returned Task. How then, am I supposed to detect errors? Surely, there should be some indication that I tried to send data over a connection that had been hung up on. Here is my latest code attempt: using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System

TaskEx.Yield(TaskScheduler)

半腔热情 提交于 2019-12-23 17:20:10
问题 Last month I asked the following question which resulted in my learning of TaskEx.Yield : Can async methods have expensive code before the first 'await'? However, I have since realized that this method actually submits all subsequent code to the ambient TaskScheduler . In true DI spirit, our team has agreed to avoid using ambient instances where possible, so I would like to know if it's possible to explicitly specify a TaskScheduler to use? Something like the following would be great: public

TPL Dataflow and exception handling in downstream blocks

耗尽温柔 提交于 2019-12-23 16:27:00
问题 I have the following pseudo code: var queue = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 5 }); var a = new ActionBlock<int>(async item => { await Task.Delay(500); Trace.TraceInformation( $"Target 1: | Type: {typeof(int).Name} | Thread: {Thread.CurrentThread.ManagedThreadId} | Message: {item}"); // handling some logic but it throws if (item >= 5) throw new Exception("Something bad happened"); }, new ExecutionDataflowBlockOptions { BoundedCapacity = 1,

How to switch between async & sync in .NET/C#

若如初见. 提交于 2019-12-23 15:55:30
问题 How do I go about switching between async/sync processing of multiple tasks in C#? I'd like to be able to switch between parallel and synchronous processing of tasks for testing/profiling purposes. Initially I had an array of Tasks initialised inline. While this was fine for parallel, I found my synchronous code wouldn't work because each method would be activated on initialization of the array. I decided to try Lazy instead. Is this code correct? If not, how is this best achieved? var