task-parallel-library

Should I always use Task.Delay instead of Thread.Sleep? [duplicate]

筅森魡賤 提交于 2019-12-30 10:05:04
问题 This question already has answers here : When to use Task.Delay, when to use Thread.Sleep? (4 answers) Closed 4 years ago . I have recently seen several recommendations stating that Thread.Sleep should never be used in production code (most recently in this SO question). Many of these advocate for using Task.Delay instead. Most of the explanations I've found use UI applications as examples, since the advantages to Task.Delay are obvious (not blocking the UI). In my case, I am using Thread

How to get maximum degree of parallelism for task parallel library usage?

痞子三分冷 提交于 2019-12-30 09:44:11
问题 I want to use Parallel.invoke. If i assign 20 parallel task, only 8 of then are run concurrently. My CPU is http://ark.intel.com/products/47925 and the reported number of threads is 8. I assume number of task can be run in parallel is related to the cpu number of threads. I dont want to create more task than the number of threads. How do i know the number of threads in c#? I tried query ParallelOptions.MaxDegreeOfParallelism and all i get is -1. 回答1: Parallel tasks are basically threads that

StackExchange.Redis Deadlocking

心已入冬 提交于 2019-12-30 08:34:13
问题 I'm using StackExchange.Redis (SE.R henceforth) in my Nancy application. There is a single global ConnectionMultiplexer that gets automatically passed around by Nancy's TinyIoC via constructor parameters, and any time I try and use GetDatabase and one of the *Async methods (sync methods only start failing after one of the async methods have been attempted) my application deadlocks. Looking at my parallel stacks it appears that I have four threads: The thread that called Result on one of my

StackExchange.Redis Deadlocking

混江龙づ霸主 提交于 2019-12-30 08:34:12
问题 I'm using StackExchange.Redis (SE.R henceforth) in my Nancy application. There is a single global ConnectionMultiplexer that gets automatically passed around by Nancy's TinyIoC via constructor parameters, and any time I try and use GetDatabase and one of the *Async methods (sync methods only start failing after one of the async methods have been attempted) my application deadlocks. Looking at my parallel stacks it appears that I have four threads: The thread that called Result on one of my

Task.Yield() versus Task.Delay(0)

本小妞迷上赌 提交于 2019-12-30 08:06:26
问题 Does Delay(0) always get inlined? In my experience, it does: using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication { class Program { static async Task Test() { await Task.Yield(); Console.WriteLine("after Yield(), thread: {0}", Thread.CurrentThread.ManagedThreadId); await Task.Delay(0); Console.WriteLine("after Delay(0), thread: {0}", Thread.CurrentThread.ManagedThreadId); await Task.Delay(100); Console.WriteLine("after Delay(100), thread: {0}",

How do I run tasks in parallel and select the first result that satisfies a given condition in C#? [duplicate]

纵饮孤独 提交于 2019-12-30 07:03:08
问题 This question already has answers here : TPL wait for task to complete with a specific return value (2 answers) Closed 4 years ago . There are three tasks that I wish to run in parallel. I wish to examine the result of the first task that finished and do a check to decide if the result is good . If yes, I cancel all other tasks and return this result, if not, I will wait for next completed task and check if that is good and do the same if it is. (Think of being good as some simple check on a

Calling Task.wait may not wait if the task has not yet started?

白昼怎懂夜的黑 提交于 2019-12-30 06:57:09
问题 I was reading Jeffrey Richter's clr via c# book and felt uncomfortable reading that task wait may not always wait and I quote "When a thread calls the Wait method, the system checks if the Task that the thread is waiting for has started executing. If it has, then the thread calling Wait will block until the Task has completed running. But if the Task has not started executing yet, then the system may (depending on the TaskScheduler ) execute the Task by using the thread that called Wait . If

Paralell.ForEach with HttpClient and ContinueWith

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 06:56:18
问题 I have a method that attempts to download data from several URLs in Parallel, and return an IEnumerable of Deserialized types The method looks like this: public IEnumerable<TContent> DownloadContentFromUrls(IEnumerable<string> urls) { var list = new List<TContent>(); Parallel.ForEach(urls, url => { lock (list) { _httpClient.GetAsync(url).ContinueWith(request => { var response = request.Result; //todo ensure success? response.Content.ReadAsStringAsync().ContinueWith(text => { var results =

Task chaining without TaskCompletionSource?

牧云@^-^@ 提交于 2019-12-30 06:50:23
问题 I'm converting some async/await code to chained tasks, so I can use it in the released framework. The await code looks like this public async Task<TraumMessage> Get() { var message = await Invoke("GET"); var memorized = await message.Memorize(); return memorized; } where Task<TraumMessage> Invoke(string verb) {} Task<TraumMessage> Memorize() {} I was hoping to chain Invoke and Memorize to return the task produced by Memorize , but that results in a Task<Task<TraumMessage> . The solution i've

Does the Task Parallel Library (or PLINQ) take other processes into account?

随声附和 提交于 2019-12-30 02:57:04
问题 In particular, I'm looking at using TPL to start (and wait for) external processes. Does the TPL look at total machine load (both CPU and I/O) before deciding to start another task (hence -- in my case -- another external process)? For example: I've got about 100 media files that need to be encoded or transcoded (e.g. from WAV to FLAC or from FLAC to MP3). The encoding is done by launching an external process (e.g. FLAC.EXE or LAME.EXE). Each file takes about 30 seconds. Each process is