WebNov 10, 2024 · I have a list of tasks that I would like to run in parallel. When one completes, I would like to examine the result of that one task. If it is successful, I would like to end all the other tasks, which I think should be no problem using a CancellationToken. If the task is unsuccessful, I'd like to go on waiting for the other tasks. WebMay 12, 2016 · Status – We can check for the status property of the task and if is faulted which means that task has not completed in usual way as shown in the below code. task.ContinueWith (t => { if (t.Status == TaskStatus.Faulted) { Console.WriteLine (t.Exception); } else { Console.WriteLine (t.Result); } });
c# - How do I get the result or return value of a …
WebMay 11, 2024 · C# Task task1 = Task.Run ( () => 1); Task task2 = Task.Run ( () => "meziantou"); await Task.WhenAll (task1, task2); var task1Result = task1.Result; // or await task1 var task2Result = task2.Result; // or await task2 I don't really want write this kind of code. Instead, I would like to get the results directly from the WhenAll method. WebSetResult (TResult result): This method is used to set the underlying Task into the RanToCompletion State. Here, the parameter result specifies the result value to bind to … byd thai
c# - Get results after Task.WhenAll() call - Stack Overflow
WebSetResult (TResult result): This method is used to set the underlying Task into the RanToCompletion State. Here, the parameter result specifies the result value to bind to this Task. Example to Understand How to Control the Result of a Task in C#? Let us understand this with an example. WebMay 9, 2024 · Only call async code only from async code. (dont mix sync with async) Never block in async code. (never .Result, never lock) If you need a lock, use SemaphoreSlim.WaitAsync () Use async/await when ... You have defined a List of Tasks that do not return anything. What you will need to do is specify the return type in the Task when you define Task as the generic type for the List in your case. Something like: var taskLists = new List>> (); This is how you specify the return type for a Task Share Improve this answer Follow byd thailand เปิดตัว