task-parallel-library

Is there a variant of `Task.Delay` that expires after real time passes e.g. even when the system is suspended and resumed?

China☆狼群 提交于 2020-01-03 08:58:05
问题 I have a situation where it makes more sense to me to have a delay between periodic actions wait for an amount of time in the real world to pass rather than waiting for the system clock to tick some number of times. This way I could, say, renew a lease being tracked on a different system/being timed out in real time after some amount of real time passes. I suspected that Task.Delay might already have this behavior, but I wanted to make sure, so I wrote a test program (see below). My discovery

Confusion about calling CPU-bound methods synchronously from an async method

孤街醉人 提交于 2020-01-03 08:25:08
问题 I'm getting my feet wet with .NET 4.5's async/await construct. I'm working on a RESTful Web API solution. I'm trying to figure out what to do with CPU-bound operation - 1) call it synchronously from the current thread, or 2) use Task.Run() ? Let's use the example from this page: async Task<int> AccessTheWebAsync() { // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); // GetStringAsync returns a Task<string>. That means that when you await

Why does the Task.WhenAny not throw an expected TimeoutException?

孤街浪徒 提交于 2020-01-03 07:30:54
问题 Please, observe the following trivial code: class Program { static void Main() { var sw = new Stopwatch(); sw.Start(); try { Task.WhenAny(RunAsync()).GetAwaiter().GetResult(); } catch (TimeoutException) { Console.WriteLine("Timed out"); } Console.WriteLine("Elapsed: " + sw.Elapsed); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); } private static async Task RunAsync() { await Observable.StartAsync(async ct => { for (int i = 0; i < 10; ++i) { await Task.Delay(500, ct); Console

Gracefully killing a TPL Task

你离开我真会死。 提交于 2020-01-03 04:48:11
问题 I'm attempting to build an API like thing against a set of REST services. There are basically different operations that report their progress and support cancelation. I opted to use the TPL to make async functions and managed load. Here is my most basic operation that is to be inherited accordingly: using FileOnlineCore.Enums; using FileOnlineCore.EventArguments; using FileOnlineCore.Events; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using

Task.Factory.StartNew with async lambda and Task.WaitAll

寵の児 提交于 2020-01-02 23:50:31
问题 I'm trying to use Task.WaitAll on a list of tasks. The thing is the tasks are an async lambda which breaks Tasks.WaitAll as it never waits. Here is an example code block: List<Task> tasks = new List<Task>(); tasks.Add(Task.Factory.StartNew(async () => { using (dbContext = new DatabaseContext()) { var records = await dbContext.Where(r => r.Id = 100).ToListAsync(); //do long cpu process here... } } Task.WaitAll(tasks); //do more stuff here This doesn't wait because of the async lambda. So how

async/await thread transition curiosity

只愿长相守 提交于 2020-01-02 12:24:51
问题 I have the following simple console application: class Program { private static int times = 0; static void Main(string[] args) { Console.WriteLine("Start {0}", Thread.CurrentThread.ManagedThreadId); var task = DoSomething(); task.Wait(); Console.WriteLine("End {0}", Thread.CurrentThread.ManagedThreadId); Console.ReadLine(); } static async Task<bool> DoSomething() { times++; if (times >= 3) { return true; } Console.WriteLine("DoSomething-1 sleeping {0}", Thread.CurrentThread.ManagedThreadId);

Is Parallel.forEach(DataTable.AsEnumerable() thread safe

荒凉一梦 提交于 2020-01-02 10:03:58
问题 when used as follows Parallel.ForEach(DataTable.AsEnumerable(), dr => { string str = dr["field1"].ToString(); //.... other stuff dr["f1"] = o.A; dr["f2"] = o.B; dr["f3"] = o.C; }); where each thread works on its own datarow I would assume not but but there's this saying about assumptions.... 回答1: The documentation of the DataRow class definitively states that This type is safe for multithreaded read operations. You must synchronize any write operations. Can't get any more specific than that.

Why are Task objects not reusable?

久未见 提交于 2020-01-02 09:31:55
问题 This question leads me to another more general (and probably fundamental) question, Why are Task objects not reusable? Microsoft writes this statement without explanation: A task may only be started and run only once. Any attempts to schedule a task a second time will result in an exception. Is the reasoning behind this so obvious that it deserves no explanation? Is there no performance hit for repeatedly setting and starting a Task with a Continuation? 回答1: A Task is a representation of a

Aggregation and Joins (Inner, Outer, Left, …) with TPL-Dataflow?

那年仲夏 提交于 2020-01-02 07:12:53
问题 Is there any better way to implement functions like aggregation within a TPL-Dataflow mesh than using a BatchBlock to buffer all items until completion, emit them as a collection and then use a transform block to do the actual aggregation? Similarly, is there any other way to do an inner/outer/left/right join of two datasets without using a BatchedJoinBlock to buffer all items of both datasources, emit them as a tuple of two collections and then do the actual join with a Transform block? 回答1:

What is the performance differences between using parallel.foreach and task inside foreach loop?

风格不统一 提交于 2020-01-02 05:54:14
问题 I would like to know what is the best way or are there any documents/articles that can help me to identify what is the differences of using Parallel.foreach and Task within a normal for each loop, like the following: case 1 - Parallel.foreach: Parallel.foreach { // Do SOmething thread safe: parsing an xml and then save // into a DB Server thry respoitory approach } case 2 - Task within foreach: foreach { Task t1 = Task.factory.startNew(()=> { //Do the same thing as case 1 that is thread safe