task-parallel-library

Task.Run with Input Parameters and Output

主宰稳场 提交于 2020-06-17 03:21:25
问题 This works great: private void MainMethod() { Task<bool> taskItemFound = new Task<bool>(ItemFound); } private bool ItemFound() { //Do Work return true; } This works but is UGLY and I can't pass more than one parameter: private void MainMethod() { var startNew = Task<bool>.Factory.StartNew(TempMethod, "cow"); } private bool TempMethod(object o) { return ("holy " + o == "holy cow"); } I'm looking for a solution that will give me a Task<bool> from an existing method with more than one input

Task.Run with Input Parameters and Output

别等时光非礼了梦想. 提交于 2020-06-17 03:20:49
问题 This works great: private void MainMethod() { Task<bool> taskItemFound = new Task<bool>(ItemFound); } private bool ItemFound() { //Do Work return true; } This works but is UGLY and I can't pass more than one parameter: private void MainMethod() { var startNew = Task<bool>.Factory.StartNew(TempMethod, "cow"); } private bool TempMethod(object o) { return ("holy " + o == "holy cow"); } I'm looking for a solution that will give me a Task<bool> from an existing method with more than one input

What is correct way to combine long-running tasks with async / await pattern?

假如想象 提交于 2020-06-10 02:20:48
问题 I have a "High-Precision" timer class that I need to be able to be start, stop & pause / resume. To do this, I'm tying together a couple of different examples I found on the internet, but I'm not sure if I'm using Tasks with asnyc / await correctly. Here is my relevant code: //based on http://haukcode.wordpress.com/2013/01/29/high-precision-timer-in-netc/ public class HighPrecisionTimer : IDisposable { Task _task; CancellationTokenSource _cancelSource; //based on http://blogs.msdn.com/b

How to cancel a CancellationToken

让人想犯罪 __ 提交于 2020-06-09 15:28:08
问题 I start a task, that start other tasks and so forth. Given that tree, if any task fails the result of the whole operation is useless. I'm considering using cancellation tokens. To my surprise, the token does not have a "CancelThisToken()" method... So my question is: how can I, in possession of ONLY a CancellationToken, cancel it? 回答1: As the docs state you need to call the cancel method from the source object. Example code is included in the link you provided. Here are the relevant sections:

How to cancel a CancellationToken

穿精又带淫゛_ 提交于 2020-06-09 15:26:51
问题 I start a task, that start other tasks and so forth. Given that tree, if any task fails the result of the whole operation is useless. I'm considering using cancellation tokens. To my surprise, the token does not have a "CancelThisToken()" method... So my question is: how can I, in possession of ONLY a CancellationToken, cancel it? 回答1: As the docs state you need to call the cancel method from the source object. Example code is included in the link you provided. Here are the relevant sections:

How to cancel a CancellationToken

跟風遠走 提交于 2020-06-09 15:26:20
问题 I start a task, that start other tasks and so forth. Given that tree, if any task fails the result of the whole operation is useless. I'm considering using cancellation tokens. To my surprise, the token does not have a "CancelThisToken()" method... So my question is: how can I, in possession of ONLY a CancellationToken, cancel it? 回答1: As the docs state you need to call the cancel method from the source object. Example code is included in the link you provided. Here are the relevant sections:

Is having a return type of Task enough to make a method run asynchronously?

大城市里の小女人 提交于 2020-06-09 07:10:20
问题 I have a simple method that does a complicated string operation and returns the result. As you can see, the return type of this method is Task<string> . Therefore, I can use Task.FromResult(result) to return the value of my string. public Task<string> ComplexOperation() { string result = // Do something complex return Task.FromResult(result); } Then, I can use the await keyword to call this method. public static async Task Main(string[] args) { var myResult = await ComplexOperation(); }

Does or does Task.Wait not start the task if it isn't already running?

烈酒焚心 提交于 2020-05-29 11:00:14
问题 According to the Microsoft TPL documentation I read (link) calling the Task.Wait() method will block the current thread until that task finishes (or cancels, or faults). But it also said that if the task in question hadn't started yet, the Wait method will attempt to run it on its own thread by asking the scheduler to reassign it, thus reducing the amount of wastage due to blocking. I've got a system in which tasks (once running) begin by collecting data by starting other tasks and waiting on

Mediator deadlock on async await within background worker - how to detect thread calling itself

大兔子大兔子 提交于 2020-05-28 06:50:13
问题 I have a mediator which I have recently needed to synchronize one at a time message dispatch on a background thread but it is locking, demonstrated below. I post a command to a queue and return a task from a TaskCompletionSource: public Task<object> Send(object command, CancellationToken cancellationToken) { var item = new CommandItem() { Command = request, Tcs = new TaskCompletionSource<object>(), Ct = cancellationToken }; this.queue.Writer.WriteAsync(item); // just write and immediatly

Ignore the Tasks throwing Exceptions at Task.WhenAll and get only the completed results

ぐ巨炮叔叔 提交于 2020-05-25 10:25:13
问题 I am working on a Task parallel problem that I have many Tasks that may or may not throw Exception. I want to process all the tasks that finishes properly and log the rest. The Task.WhenAll propage the Task exception without allowing me to gather the rest results. static readonly Task<string> NormalTask1 = Task.FromResult("Task result 1"); static readonly Task<string> NormalTask2 = Task.FromResult("Task result 2"); static readonly Task<string> ExceptionTk = Task.FromException<string>(new