task-parallel-library

Is this a correct .NET 4 implementation for .NET 4.5's WhenAll?

ⅰ亾dé卋堺 提交于 2019-12-23 15:03:50
问题 I am using SignalR. The function on the Hub often return a Task. I now have a function that will add a connection to a bunch of groups. I would like to return a Task that represents all of these Tasks. I found a perfect function for that: Task.WhenAll. However this is a new function in .NET 4.5 and I am still stuck on .NET 4. Hence I decided to write my own version of it until we can move to .NET 4.5. Because there are often some caveats when it comes to multithreading (e.g. thread pool stuff

Task.Run with cancellation support

好久不见. 提交于 2019-12-23 14:23:05
问题 Consider this Task.Run example. It shows how to create a task with cancellation support. I am doing something similar: Task.Run(()=>{while (!token.IsCancellationRequested()) {...}}, token); My questions: Since I already have a reference to the cancellation token, why the purpose of passing it as a parameter to the Task.Run invocation? I often see the following code in examples: if (token.IsCancellationRequested) token.ThrowIfCancellationRequested(); What's the purpose of this code? Why not

Why does TaskFactory.StartNew receive a CancellationToken [duplicate]

怎甘沉沦 提交于 2019-12-23 13:08:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Cancellation token in Task constructor: why? This method receives a CancellationToken: CancellationTokenSource cts = new CancellationTokenSource(4); var t = Task.Factory.StartNew(() => { // code }, cts.Token); Since cancellation is cooperative (the actual working code needs to observe the cancellation token), What is the purpose of passing this to the StartNew method as an argument? 回答1: It allows the task

How do I make non-essential computations run in the background?

为君一笑 提交于 2019-12-23 12:41:04
问题 This question is in follow up to Why is this code running synchronously? . I realize that my real question is at a higher level than the one in that post. The question I now ask, below, is "how do I accomplish this?" I want to use concurrency in C# to compute things in the background. I have class ptsTin, which represents an existing ground surface. I want to make loading as fast as possible. Some of the work is essential in that you don't have an instance until the work is complete. For

Migrating lock to TPL

别等时光非礼了梦想. 提交于 2019-12-23 12:08:33
问题 In normal C# we write int DoSomething(){/*...*/)}; lock(mutex) { return DoSomething(); } to ensure in all cases the mutex is released. But if the signature of DoSomething changed to Task<int> DoSomeThingAsync(){/*...*/}; Does the following code return Task.Factory.StartNew(() => { Monitor.Enter(mutex); return DoSomethingAsync(); }).Unwrap().ContinueWith(t => { Monitor.Exit(mutex); return t; }).Unwrap(); do similar things? Is it guaranteed to release the mutex whenever it was entered? Are

Run Multiple Tasks (Variable Number) in parallel and continue when all have finished

落花浮王杯 提交于 2019-12-23 12:07:09
问题 I need to start a "number" of tasks (variable but less than 10) not in parallel, and wait for them all to finish, getting from each the result. I'm getting the result from each of them, saving in a list and then using it in the end. Here's my code, and it's working but I think there gotta be a cleaner way to do that. CAUSING THE NUMBER OF TASKS List<String> Arguments = new List<String> { "AA", "BB", "CC" }; List<String> ResultList = new List<String>(); //**AT LEAST I'VE GOT ONE** Task<String>

Run Multiple Tasks (Variable Number) in parallel and continue when all have finished

只谈情不闲聊 提交于 2019-12-23 12:06:12
问题 I need to start a "number" of tasks (variable but less than 10) not in parallel, and wait for them all to finish, getting from each the result. I'm getting the result from each of them, saving in a list and then using it in the end. Here's my code, and it's working but I think there gotta be a cleaner way to do that. CAUSING THE NUMBER OF TASKS List<String> Arguments = new List<String> { "AA", "BB", "CC" }; List<String> ResultList = new List<String>(); //**AT LEAST I'VE GOT ONE** Task<String>

Does C# Parallel.ForEach use the same thread for iterations of the collection

a 夏天 提交于 2019-12-23 09:58:10
问题 I have an IEnumerable that I want to pass to Parallel.ForEach, but the IEnumerable is implemented using a C# method and yield, and potentially the code in enumerator is not thread safe. Was planning to use Parallel.ForEach on this. I am under the assumption that the actual internal iteration of the IEnumerable by ForEach is always on the same thread, and then the ForEach passes each item to a potential other thread for the processing. Is this correct? I read where IEnumerable iteration uses

Async/Await or Task.Run in Console Application/Windows Service

我们两清 提交于 2019-12-23 09:57:40
问题 I have been researching (including looking at all other SO posts on this topic) the best way to implement a (most likely) Windows Service worker that will pull items of work from a database and process them in parallel asynchronously in a 'fire-and-forget' manner in the background (the work item management will all be handled in the asynchronous method). The work items will be web service calls and database queries. There will be some throttling applied to the producer of these work items to

Why is the culture kept after ConfigureAwait(false)

久未见 提交于 2019-12-23 09:34:07
问题 I have the following async code: // Main system culture is English here Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("es"); WriteLine($"{Thread.CurrentThread.ManagedThreadId}:Culture:{Thread.CurrentThread.CurrentCulture}"); await Task.Delay(1).ConfigureAwait(false); WriteLine($"{Thread.CurrentThread.ManagedThreadId}:Culture:{Thread.CurrentThread.CurrentCulture}"); The resul I expected would be to have a different thread id after the await and that new thread id having the