task-parallel-library

Parallel Foreach webservice call

强颜欢笑 提交于 2019-12-24 19:29:59
问题 We have implemented application wherein we need to process incoming batch. for example a set of Request of certain object type has to be sent to particular webservice to have it processed We have implemented following snippet to do so. need your help / guidance if there wouldbe any pitfalls on same var options = new ParallelOptions { MaxDegreeOfParallelism = 10 }; Parallel.ForEach(request, options, currentRequest => { ProcessedRequest processedRequest = null; try { currentRequest.DBSave =

SynchronizationLockException on Monitor.Exit when using await

爷,独闯天下 提交于 2019-12-24 16:35:10
问题 I am creating a piece of code that gets a webpage from a legacy system we have. In order to avoid excessive querying, I am caching the obtained URL. I am using Monitor.Enter , Monitor.Exit and double checking to avoid that request is issued twice, but when releasing the lock with Monitor.Exit , I am getting this exception: System.Threading.SynchronizationLockException was caught HResult=-2146233064 Message=Object synchronization method was called from an unsynchronized block of code. Source

Start, Stop, Pause and Continue a very long running method

我们两清 提交于 2019-12-24 12:35:18
问题 I have a method which is running very long. I want to be able to stop, pause and continue the execution of that method.I viewed this question and the answer is nice, but I want to use more general library like TPL. TPL has a cancellation mechanism, but I can't find pause/continue functionality. edit Thanks for all answers. But the problem is that I don't write that "long running" method, I just call that method. 回答1: The easiest way to implement this is with a BackgroundWorker control. It

Limit number of tasks with TPL

荒凉一梦 提交于 2019-12-24 12:28:38
问题 I'm looking into creating tasks which need to be executed on multiple threads. I could have a large number of tasks being created i.e. 2000 for example. I want to limit the number of tasks queued and executed simultaneously. Is there a way to create a certain number of tasks and then create new ones as they complete? Trying to work out if the task scheduler helps with this. EDIT: To phrase this a different way...is there a reason I should want to limit the number of tasks created/queued

async library calls relying on each other and how to handle?

孤街醉人 提交于 2019-12-24 11:33:44
问题 this is a follow on from a previous question I posted Calling an async method using a Task.Run seems wrong? I thought the code written by the contractor was wrong but following on from the answers provided I'm now wondering if it's the libraries fault. This library exposes two methods that I need to use. One returns a "template" and one consumes part of this template (it does in my implementation anyway). But both are async methods returning Tasks . To explain my library has methods: public

TPL - set Task to Thread.Sleep for a long time

自古美人都是妖i 提交于 2019-12-24 11:06:24
问题 I have a test to use .NET Task Parallel Library: static void Main(string[] args) { for(int i = 0; i < 1000; i++) { int n = i; Task.Factory.StartNew(() => TaskTest(n)); } } static void TaskTest(int i) { // Will sleep for a long time Thread.Sleep(TimeSpan.FromMinutes(i)); // Do something here } One thing I'm not sure: When Thread.Sleep in the above code execute, what will happen? I know it will not occupy a thread in the ThreadPool, is there any drawback if I set multiple tasks to Thread.Sleep

Exception not caught in Task.Run wrapped method

痴心易碎 提交于 2019-12-24 10:48:06
问题 New to async await integration in C# 5. I'm working with some basic Task based methods to explore async await and the TPL. In this example below I'm calling a web service with a timeout of 5 seconds. If the timeout expires it should throw an exception so I can return false from the method. However, the timeout never occurs, or maybe it does but the Task never returns. public static Task<bool> IsConnectedAsync() { return Task.Run(() => { try { using (WSAppService.AppService svc = new

use Task.Run() inside of Select LINQ method

江枫思渺然 提交于 2019-12-24 10:39:52
问题 Suppose I have the following code (just for learninig purposes): static async Task Main(string[] args) { var results = new ConcurrentDictionary<string, int>(); var tasks = Enumerable.Range(0, 100).Select(async index => { var res = await DoAsyncJob(index); results.TryAdd(index.ToString(), res); }); await Task.WhenAll(tasks); Console.WriteLine($"Items in dictionary {results.Count}"); } static async Task<int> DoAsyncJob(int i) { // simulate some I/O bound operation await Task.Delay(100); return

Do I need to terminate Task in windows service in OnStop method?

我怕爱的太早我们不能终老 提交于 2019-12-24 09:18:52
问题 So I have a Windows Service And I started task in OnStart partial class AppServices : ServiceBase { private static readonly ILog Log = LogManager.GetCurrentClassLogger(); private Task _task; public AppServices() { InitializeComponent(); } protected override void OnStart(string[] args) { Log.Info("Service started"); _task = Task.Factory.StartNew(StartWork, TaskCreationOptions.LongRunning); } protected override void OnStop() { Log.Info("Service stopped"); } private void StartWork() { while(true

Task.WaitAll hang when one task throws and another syncs with the first task

爷,独闯天下 提交于 2019-12-24 08:48:01
问题 What should be expected from Task.WaitAll(t1, t2) , when t2 's execution syncs with t1 , but t1 throws an exception before the point it syncs to t2 ? In this case, it's obvious that t2 will never finish. However since t1 throws an exception, I expected Task.WaitAll(t1,t2) to return with aggregated exception indicating one of the task fails. However this is not the case. It turns out that Task.WaitAll hangs waiting forever. I can argue for this behavior that Task.WaitAll does what it claims to