task-parallel-library

Why Garbage Collector doesn't collect Tasks objects

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-23 13:24:08
问题 especially when no live Thread reference it. I thought GC goes thought all .net threads to find references... Does it check references in other places too? EDIT: FOr instance let's imagine we are in a console app, the main calls a method that creates a local task1, then applies a task1.ContinueWith(task2) and returns to main, main do console.readline(). At this point it could be that task1 has finished, task2 still has not started a GC could start and no thread has a reference to task2. Why

Wrapping blocking calls to be async for better thread reuse and responsive UI

孤街醉人 提交于 2020-01-23 12:16:09
问题 I have a class that is responsible for retrieving a product availability by making call to a legacy class. This legacy class itself internally collects product data by making BLOCKING network calls. Note that I cannot modify code of legacy API. Since all products are independent to each other, I would like to parallelise collecting the information without creating any unnecessary threads and also not blocking thread that gets blocked on calling this legacy API. With this background here are

To Task.Run or not to Task.Run

∥☆過路亽.° 提交于 2020-01-23 06:49:48
问题 Suppose I have an interface which includes an async method, and I have two different implementations of that interface. One of the two implementations is naturally async, and the other is not. What would be the "most correct" way of implementing the non-async method? public interface ISomething { Task<Foo> DoSomethingAsync(); } // Normal async implementation public class Implementation1 : ISomething { async Task<Foo> ISomething.DoSomethingAsync() { return await DoSomethingElseAsync(); } } //

How can I monitor the Task queues in the .NET TaskSchedulers (across AppDomain)

十年热恋 提交于 2020-01-23 01:22:06
问题 As a developer, I would like to monitor the size (and progress) of work in the Task Queues in the TaskSchedulers so that I can evaluate whether an experienced slowdown under production load is due to the magnitude or perhaps stall of tasks scheduled. Normally I would simply attach the debugger and inspect task sizes, but: The application is running under mono and in production, so I cannot attach to it using visual studio I would like to deliver output of analysis on this data as surveillance

Why use C# async/await for CPU-bound tasks [closed]

十年热恋 提交于 2020-01-22 19:38:24
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I'm getting the hang of the async/await keywords in C#, and how they facilitate asynchronous programming - allowing the the thread to be used elsewhere whilst some I/O bound task like a db call is going on. I have read numerous times that async/await is for I/O bound tasks,

Parallel.Invoke - Exception handling

坚强是说给别人听的谎言 提交于 2020-01-22 14:52:10
问题 My code runs 4 function to fill in information (Using Invoke) to a class such as: class Person { int Age; string name; long ID; bool isVegeterian public static Person GetPerson(int LocalID) { Person person; Parallel.Invoke(() => {GetAgeFromWebServiceX(person)}, () => {GetNameFromWebServiceY(person)}, () => {GetIDFromWebServiceZ(person)}, () => { // connect to my database and get information if vegeterian (using LocalID) .... if (!person.isVegetrian) return null .... }); } } My question is: I

Different exception handling between Task.Run and Task.Factory.StartNew

蓝咒 提交于 2020-01-22 10:53:57
问题 I encountered an issue when I was using Task.Factory.StartNew and tried to capture an exception that is thrown. In my application I have a long running task that I want to encapsulate in a Task.Factory.StartNew(.., TaskCreationOptions.LongRunning); However, the exception isn't caught when I'm using Task.Factory.StartNew . It is however working as I expect when I use Task.Run , which I thought was just a wrapper on Task.Factory.StartNew (according to for instance this MSDN article). A working

Different exception handling between Task.Run and Task.Factory.StartNew

筅森魡賤 提交于 2020-01-22 10:53:12
问题 I encountered an issue when I was using Task.Factory.StartNew and tried to capture an exception that is thrown. In my application I have a long running task that I want to encapsulate in a Task.Factory.StartNew(.., TaskCreationOptions.LongRunning); However, the exception isn't caught when I'm using Task.Factory.StartNew . It is however working as I expect when I use Task.Run , which I thought was just a wrapper on Task.Factory.StartNew (according to for instance this MSDN article). A working

Why does this async/await code NOT cause a deadlock?

拈花ヽ惹草 提交于 2020-01-21 12:13:31
问题 I have found the following example in Jon Skeet's "C# in depth. 3rd edition": static async Task<int> GetPageLengthAsync(string url) { using (HttpClient client = new HttpClient()) { Task<string> fetchTextTask = client.GetStringAsync(url); int length = (await fetchTextTask).Length; return length; } } public static void Main() { Task<int> lengthTask = GetPageLengthAsync("http://csharpindepth.com"); Console.WriteLine(lengthTask.Result); } I expected that this code would deadlock, but it does not.

Async logging throwing a NullReferenceException

点点圈 提交于 2020-01-21 03:06:27
问题 I am trying to asynchronously log some information to SQL Server inside of an MVC 4 controller action targeting .NET 4.0 using the AsyncTargetingPack. I would jump straight to .NET 4.5 but my app lives in Azure and we're still waiting for the update... This code works as expected (a row is written to my database with no exceptions thrown): public class SystemActionLogger : ISystemActionLogger { private readonly ActionBlock<Tuple<SystemAction, object>> actionBlock; public SystemActionLogger