task-parallel-library

TPL Dataflow how to remove the link between the blocks

霸气de小男生 提交于 2019-12-23 09:18:11
问题 I would like to know. How can I remove the link between the blocks? In other words. I want to get opposite of LinkTo. I want to write a logger based on tlp dataflow. I wrote this interface and want to delete a subscription for ILogListener when it needed. public interface ILogManager { void RemoveListener(ILogListener listener); } 回答1: When you link blocks: var link = someSourceBlock.LinkTo(someTargetBlock); you get a return value that implements IDisposable . To kill the link, just dispose

Does a C# Task run on one core?

落花浮王杯 提交于 2019-12-23 08:57:08
问题 Does a C# Task run on one core? I have a project where I need to decide how many Tasks to create. I need to create as many, as the computer can take. Is that the number of processors, cores, or logical processors, I am confused between the three options. 回答1: A task executes on a thread. The OS schedules threads to cores. Cores are a logical concept and different from physical CPU chips. Create Environment.ProcessorCount tasks, or better use one of the higher level TPL constructs like PLINQ

Task.Faulted and Task.Exception

空扰寡人 提交于 2019-12-23 08:33:33
问题 Neither TaskStatus Enum or Task.Exception MSDN appear to state explicity: Does TasksStatus.Faulted ALWAYS imply Task.Exception != null (and TaskStatus != Faulted always imply Task.Exception == null )? 回答1: Yes the documentation for Task.IsFaulted explicitly states that: If IsFaulted is true, the task's Status will be equal to Faulted, and its Exception property will be non-null. The reference source code does list the as an almost certainly. In FinishStageTwo we see that the internal m_state

Create a Task with an Action<T>

浪子不回头ぞ 提交于 2019-12-23 07:38:35
问题 I somehow feel I am missing something basic. Here's my problem. I am trying to create a System.Threading.Tasks.Task instance to execute an action that accepts a parameter of a certain type. I thought I could do something like void DoWork(MyClass obj) {} //My action that accepts a parameter of type 'MyClass' MyClass obj = new MyClass(); Action<MyClass> action = DoWork; //action that points to the method Task task = new Task(action,obj); //task that would execute 'DoWork' with 'obj' as the

Where to use concurrency when calling an API

淺唱寂寞╮ 提交于 2019-12-23 07:16:09
问题 Inside a c# project I'm making some calls to a web api, the thing is that I'm doing them within a loop in a method. Usually there are not so many but even though I was thinking of taking advantage of parallelism. What I am trying so far is public void DeployView(int itemId, string itemCode, int environmentTypeId) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ApiUrl"]); client.DefaultRequestHeaders.Accept.Clear(); client

Parallel.ForEach Misbehaviour [duplicate]

好久不见. 提交于 2019-12-23 07:16:07
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: C# Value storage during Parallel Processing I was running some performance tests in my console application today and I stumbled across something really unexpected. My code: int iterations = 1000000; var mainList = new List<string>(); for (int i = 0; i < iterations; i++) { mainList.Add(i.ToString()); } var listA = new List<string>(); Parallel.ForEach(mainList, (listItem) => { if (Int32.Parse(listItem)%2 == 0) {

Parallel.ForEach Misbehaviour [duplicate]

元气小坏坏 提交于 2019-12-23 07:16:03
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: C# Value storage during Parallel Processing I was running some performance tests in my console application today and I stumbled across something really unexpected. My code: int iterations = 1000000; var mainList = new List<string>(); for (int i = 0; i < iterations; i++) { mainList.Add(i.ToString()); } var listA = new List<string>(); Parallel.ForEach(mainList, (listItem) => { if (Int32.Parse(listItem)%2 == 0) {

How to return result from Task<HttpResponseMessage>, to take the benefit of Async HttpClient call?

萝らか妹 提交于 2019-12-23 05:33:07
问题 Can I write code (1st) like bellow public Task<HttpResponseMessage> Get(int id) { return Task<HttpResponseMessage>.Factory.StartNew(() => Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(model))); } Can I write code (2nd) like bellow public Task<HttpResponseMessage> Put(int id, string value) { return Task<HttpResponseMessage>.Factory.StartNew(() => Request.CreateResponse(HttpStatusCode.OK)); } I want call above described Put method using Httpclient.PutAsJsonAsync() . in

Get batches of messages as available

天大地大妈咪最大 提交于 2019-12-23 03:54:07
问题 I am trying to achieve the following behaviour using the Task Parallel Library: As messages arrive I would like to process them sequentially but in groups. So when the first message arrives it should be processed immediately. If 2 messages come in while the first is being processed then they should be processed in a group of 2. I can almost get what I want using a BatchBlock linked to an ActionBlock var batchBlock = new BatchBlock<int>(100); var actionBlock = new ActionBlock<int[]>(list => {

How to safely use SmtpClient.SendAsync in Multithreaded application

十年热恋 提交于 2019-12-23 03:28:10
问题 In my application, I am using ActionBlock from Dataflow library, to send out email alerts using SmtpClient.SendAsync() method, which does not block calling thread.( ActionBlock is getting it's data from BufferBlock , and blocks are tied together using bufferBlock.LinkTo(actionBlock) ). However, this method will throw InvalidOperationException if another .SendAsync() call is in progress. According to MSDN documentation, there is public event SendCompletedEventHandler SendCompleted that is