I have some asynchronous operations that consist of two distinct stages. Initially I want to await them until the completion of their first stage, and later await them until their final completion. Here is a simplified version of these operations:
async Task<string> TwoStagesAsync()
{
Console.WriteLine($"Stage 1 Started");
await Task.Delay(1000); // Simulate an I/O operation
bool resultOfStage1 = true;
Console.WriteLine($"Stage 1 Finished");
if (!resultOfStage1) return null;
/* Stage separator */
Console.WriteLine($"Stage 2 Started");
await Task.Delay(1000); // Simulate an I/O operation
Console.WriteLine($"Stage 2 Finished");
return "Hello!";
}
To achieve this requirement I had the idea of representing these two-stage operations as nested tasks: Task<Task<string>>. This would allow me to await initially the outer task, and later await the result of the outer task, which would be the inner task. This is my currently best attempt to implement this idea:
async Task<Task<string>> TwoStagesNestedAsync_A() // Problematic
{
Console.WriteLine($"Stage 1 Started");
await Task.Delay(1000); // Simulate an I/O operation
bool resultOfStage1 = true;
Console.WriteLine($"Stage 1 Finished");
if (!resultOfStage1) return Task.FromResult((string)null);
/* Stage separator */
return Task.Run(async () =>
{
Console.WriteLine($"Stage 2 Started");
await Task.Delay(1000); // Simulate an I/O operation
Console.WriteLine($"Stage 2 Finished");
return "Hello!";
});
}
What I like to this solution is that it works and it is quite readable, since it doesn't require any special synchronization primitives like SemaphoreSlim or TaskCompletionSource. What I don't like is that the second stage is executed in the ThreadPool context instead of the initial SynchronizationContext. Is there any way to make it use the current SynchronizationContext from start to finish, without complicating it too much?
I should include one more of my failed attempts. Replacing the Task.Run with a local async function doesn't work, because for some reason the line Console.WriteLine($"Stage 2 Started") is executed as part of the first stage, instead of the second stage.
async Task<Task<string>> TwoStagesNestedAsync_B() // Problematic
{
Console.WriteLine($"Stage 1 Started");
await Task.Delay(1000); // Simulate an I/O operation
bool resultOfStage1 = true;
Console.WriteLine($"Stage 1 Finished");
if (!resultOfStage1) return Task.FromResult((string)null);
return SecondStageAsync();
async Task<string> SecondStageAsync()
{
Console.WriteLine($"Stage 2 Started");
await Task.Delay(1000); // Simulate an I/O operation
Console.WriteLine($"Stage 2 Finished");
return "Hello!";
}
}
Update: Here is an example of consuming an asynchronous operation that consists of two stages:
Task<Task<string>>[] operations = Enumerable.Range(1, 10)
.Select(_ => TwoStagesNestedAsync_A())
.ToArray();
/* Do something else before awaiting Stage 1 */
Task<string>[] innerTasks = await Task.WhenAll(operations);
Console.WriteLine($"Stage 1 is now complete");
/* Do something else before awaiting Stage 2 */
string[] results = await Task.WhenAll(innerTasks);
Console.WriteLine($"Stage 2 is now complete");
I assume you want to execute something when first stage is complete.
You can pass an action as parameter to the function.
public async Task<string> TwoStagesAsync(Func<Task> injectedAction)
{
await ExecuteStageOne();
// Execute without "stopping" second stage
var injectedTask = injectedAction.Invoke();
if (somethingFailed) return null;
/* Stage separator */
await ExecuteStageTwo();
await injectedTask; // Make sure it completes without errors
return "Hello!";
}
After update
Requirements tell us that consumer of the TwoStages method do know that operation has two stages and this consumer want execute some action between every stage.
So we need to expose tasks of every state to the consumer.
If you wrap TwoStages method within a class, you can expose more details for its consumers.
We write code in object-oriented programming language anyway, isn't it ;)
public class TwoStageOperation
{
public TwoStageOperation() { }
public async Task ExecuteFirstStage()
{
Console.WriteLine($"Stage 1 Started");
await Task.Delay(1000);
Console.WriteLine($"Stage 1 Finished");
}
public async Task<string> ExecuteLastStage()
{
Console.WriteLine($"Stage 2 Started");
await Task.Delay(1000);
Console.WriteLine($"Stage 2 Finished");
return "Hello";
}
}
Usage
var operations = Enumerable.Range(1, 10)
.Select(_ => new TwoStageOperation())
.ToArray();
/* Do something else before awaiting Stage 1 */
await Task.WhenAll(operations.Select(op => op.ExecuteFirstStage());
Console.WriteLine($"Stage 1 is now complete");
/* Do something else before awaiting Stage 2 */
string[] results = await Task.WhenAll(operations.Select(op => op.ExecuteLastStage());
Console.WriteLine($"Stage 2 is now complete");
In case operations has different implementations, you can introduce an interface and have different implementations
public interface ITwoStageOperation
{
Task ExecuteFirstStage();
Task<string> ExecuteLastStage();
}
var operations = new ITwoStageOperation[]
{
new LandTwoStageOperation(),
new OceanTwoStageOperation(),
new AirTwoStageOperation(),
};
Alternative approach
Which I think you will prefer more, because you were very close to it :), would be to return a function as result of first stage
public async Task<Func<Task<string>>> TwoStagesAsync()
{
await ExecuteStageOne();
Func<Task<string>> lastStage = async () =>
{
await Task.Delay(1000);
return "Hello";
};
return lastStage;
}
Usage
var firstStages = Enumerable.Range(1, 10)
.Select(_ => TwoStagesAsync())
.ToArray();
/* Do something else before awaiting Stage 1 */
var lastStages = await Task.WhenAll(firstStages);
Console.WriteLine($"Stage 1 is now complete");
/* Do something else before awaiting Stage 2 */
string[] results = await Task.WhenAll(lastStages.Select(s => s.Invoke());
Console.WriteLine($"Stage 2 is now complete");
Try this.
public static async Task<bool> FirstStageAsync() // Problematic
{
Console.WriteLine($"Stage 1 Started");
await Task.Delay(1000); // Simulate an I/O operation
bool resultOfStage1 = true;
Console.WriteLine($"Stage 1 Finished");
return await Task.FromResult(resultOfStage1);
}
public static async Task<string> SecondStageAsync()
{
Console.WriteLine($"Stage 2 Started");
await Task.Delay(1000); // Simulate an I/O operation
Console.WriteLine($"Stage 2 Finished");
return "Hello!";
}
Then you can call it by:
var task = FirstStageAsync().ContinueWith(async d =>
{
if (d.IsCompleted)
{
var resultOfStage1 = await d;
if (resultOfStage1)
{
var task2 = SecondStageAsync();
task2.Wait();
}
}
});
task.Wait();
Is that you want to achieve?
Be careful with deadlock.
Good Luck!
Related
I want to start an GetAsync or PostAsync and then in a loop do something and check for results.
req1 = client.GetAsync(url_1); // a time consuming request
do
{
//do something here
var req2= await client.GetAsync(url_2);
var result2 = await req2.Content.ReadAsStringAsync();
} while (!IsResultReady(req1)); // check if url_1 job is done and stop the loop
var result1 = await req1.Content.ReadAsStringAsync();
this example should give you what you need
async Task Main()
{
var mainTask = MyLongRunningTask();
// mainTask is already started without await
do
{
await DoSomethingElse();
} while (!mainTask.IsCompleted);
}
public async Task MyLongRunningTask()
{
Console.WriteLine("Long Running Task Started");
await Task.Delay(3000); // simulating client.GetAsync(url_1)
Console.WriteLine("Long Running Task Finished");
}
async Task DoSomethingElse()
{
Console.WriteLine("doing some other tasks");
await Task.Delay(1000);
}
output:
Long Running Task Started
doing some other tasks
doing some other tasks
doing some other tasks
Long Running Task Finished
I have the following code:
//Await #1
var response1 = await doSomething();
if(response1.isSuccess) {
//Await #2
var response2 = await doSomethingElse();
}
Response 1 and response 2 are totally independent and i want to parallelize the await task here.
Basically response 2 takes a lot of time and hence is only invoked when response1 is success.
Is there any way in which i can invoke both tasks and see the result of response 1 and if it is fail, i drop/skip the response of Await#2.
Essentially what you want is to cancel a task, but with a little more logic.
You need to edit doSomethingElse so that it accepts a CancellationToken, and also so that it makes use of it to stop what its doing:
public async Task<Foo> DoSomethingElse(CancellationToken token) {
...
if (token.IsCancellationRequested) {
// stop what you are doing...
// I can't tell you how to implement this without seeing how DoSomethingElse is implemented
}
...
}
Now, get a CancellationToken from a CancellationTokenSource:
var source = new CancellationTokenSource();
var token = source.Token;
And here comes the logic of "if response 1 fails cancel response 2":
var response2Task = DoSomethingElse(token);
var response1 = await DoSomething();
if (!response1.IsSuccess) {
source.Cancel();
} else {
var response2 = await response2Task;
}
var task2 = doSomethingElse();
var response1 = await doSomething();
if(response1.isSuccess) {
var response2 = await task2;
}
This will start the execution of doSomethingElse() immediately, and only wait for its completion when response1.isSuccess == true
You can launch both threads, then once the first task obtains a result, you could either stop, or 'wait' for the second thread to finish.
The conditional logic, imo, should be placed in the first task's thread. If you do not have access to the doSomething, make a lambda in which you will await doSomething's response and then proceed with the condition logic.
How to stop a thread? Here you go, and here you go
This is what you can do:
var task1 = Task.Run(() =>
{
Console.WriteLine("running task 1");
return 1;
});
var task2 = Task.Run(() =>
{
Console.WriteLine("running task 2");
return 2;
});
await Task.WhenAll(task1, task2);
So you will run the 2 tasks in parallel. If you need to check the result of particular tasks, this is the way to do that(it won't trigger the task again if you run Task.WhenAll() previously. It will just get you the results of previously executed tasks):
var result1 = await task1;
var result2 = await task2;
Then you can apply some condition:
if(result1 == 1) return result2;
etc..
I'm not sure if I'm misunderstanding the usage of Task.WhenAny but in the following code only "0" gets printed when it should print "1" and "2" and then "mx.Name" every time a task finishes:
public async void PopulateMxRecords(List<string> nsRecords, int threads)
{
ThreadPool.SetMinThreads(threads, threads);
var resolver = new DnsStubResolver();
var tasks = nsRecords.Select(ns => resolver.ResolveAsync<MxRecord>(ns, RecordType.Mx));
Console.WriteLine("0");
var finished = Task.WhenAny(tasks);
Console.WriteLine("1");
while (mxNsRecords.Count < nsRecords.Count)
{
Console.WriteLine("2");
var task = await finished;
var mxRecords = await task;
foreach(var mx in mxRecords)
Console.WriteLine(mx.Name);
}
}
The DnsStubResolver is part of ARSoft.Tools.Net.Dns. The nsRecords list contains up to 2 million strings.
I'm not sure if I'm misunderstanding the usage of Task.WhenAny
You might be. The pattern you seem to be looking for is interleaving. In the following example, notice the important changes that I have made:
use ToList() to materialize the LINQ query results,
move WhenAny() into the loop,
use Remove(task) as each task completes, and
run the while loop as long as tasks.Count() > 0.
Those are the important changes. The other changes are there to make your listing into a runnable demo of interleaving, the full listing of which is here: https://dotnetfiddle.net/nr1gQ7
public static async Task PopulateMxRecords(List<string> nsRecords)
{
var tasks = nsRecords.Select(ns => ResolveAsync(ns)).ToList();
while (tasks.Count() > 0)
{
var task = await Task.WhenAny(tasks);
tasks.Remove(task);
var mxRecords = await task;
Console.WriteLine(mxRecords);
}
}
Can I await on a Task that was created on a different thread? For example:
...
CurrentIteration = Execute(); // returns Task
await CurrentIteration;
...
And then, on another thread:
...
await CurrentIteration;
...
Will the second thread wait for method Execute to finish executing?
If it will, will I be able to re-use CurrentIteration for the same purpose in the second thread, given that I re-run
CurrentIteration = Execute(); // returns Task
await CurrentIteration;
On the first thread?
I tried this code:
public class Program
{
public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
public static async Task MainAsync(string[] args)
{
var instance = new SomeClass();
var task = instance.Execute();
Console.WriteLine("thread 1 waiting...");
Task.Run(async () =>
{
Console.WriteLine("thread 2 started... waiting...");
await task;
Console.WriteLine("thread 2 ended!!!!!");
});
await task;
Console.WriteLine("thread 1 done!!");
Console.ReadKey();
}
}
public class SomeClass
{
public async Task Execute()
{
await Task.Delay(4000);
}
}
But it prints
thread 1 waiting...
thread 2 started... waiting...
then
thread 1 done!!
but never thread 2 ended!!!!!. Why is that? How can I achieve that? Thanks!
You can await on a task from multiple threads. You were actually really close to get that to work, as #Rob said, you just needed to await the second thread.
consider this:
public static async Task MainAsync(string[] args)
{
var instance = new SomeClass();
var task = instance.Execute();
Console.WriteLine("thread 1 waiting...");
var secondTask = Task.Run(async () =>
{
Console.WriteLine("thread 2 started... waiting...");
await task;
Console.WriteLine("thread 2 ended!!!!!");
});
await task;
await secondTask;
Console.WriteLine("thread 1 done!!");
Console.ReadKey();
}
Add the wait on your second thread after you finish waiting for the task.
The reason you didn't see the indication is because the console got stuck on the ReadKey method, and couldn't write anything until it's finished. If you would've pressed Enter, you can see the "thread 2 ended!!!!!" line for a second before the app closes.
i read this guide line from here http://blog.stephencleary.com/2012/02/async-and-await.html
here i got few couple of code but not very clear to me.
1)
public async Task DoOperationsInParallelAsync()
{
Task[] tasks = new Task[3];
tasks[0] = DoOperation0();
tasks[1] = DoOperation1();
tasks[2] = DoOperation2();
// At this point, all three tasks are running in parallel.
// Now, we await them all.
await Task.WhenAll(tasks);
}
in the above we are creating multiple task but suppose when all task will run parallel then DoOperation2() may be finish first and DoOperation0() and at last DoOperation1() complete. if i want to show that message like DoOperation2() is completed in console windows then how could i do this. how could i detect that which task complete when multiple is running.
2) when we run any function with the help of async/await the does it run as background thread or foreground thread.
3)
public async Task<int> GetFirstToRespondAsync()
{
// Call two web services; take the first response.
Task<int>[] tasks = new[] { WebService1(), WebService2() };
// Await for the first one to respond.
Task<int> firstTask = await Task.WhenAny(tasks);
// Return the result.
return await firstTask;
}
i do not understand why the person wrote Await for the first one to respond.
// Await for the first one to respond.
Task firstTask = await Task.WhenAny(tasks);
why first one...why not second one because two task are running here.
please guide me and drive out my confusion. thanks
Because it's a console app, you need to wait for the task to finish. Here's an example of how to return a string from a task:
class WhenAny
{
public static async Task<string> GetFirstToRespondAsync()
{
// Call two web services; take the first response.
Task<string>[] tasks = new[] { Task1(), Task2() };
// Await for the first one to respond.
Task<string> firstTask = await Task.WhenAny(tasks);
// Return the result.
return firstTask.Result;
}
private static async Task<string> Task1()
{
await Task.Delay(3000);
return "Task1";
}
private static async Task<string> Task2()
{
await Task.Delay(1000);
return "Task2";
}
}
Call that from the Main function as follows:
static void Main(string[] args)
{
var t = WhenAny.GetFirstToRespondAsync();
t.ContinueWith((taskName) =>
{
string result = taskName.Result;
Console.WriteLine("Result: " + result);
});
t.Wait();
Console.ReadLine();
}
That should return the task that completes first, and you can access that information from the Task.Result
Awaiting a method does not, in itself, create an additional thread. What it does is create a callback to avoid blocking the current thread (typically this is used to not block the UI thread).
WhenAny returns when the earliest completed operation returns. That doesn't mean the first in the list that you provide. So, the code above will always show 1000, even though it's the second task.
For completeness, here's the same thing with WhenAll:
class WhenAll
{
public static async Task<string[]> WaitForAllAsync()
{
// Call two web services; take the first response.
Task<string>[] tasks = new[] { Task1(), Task2(), Task3() };
// Wait for a tasks
string[] results = await Task.WhenAll(tasks);
// Return the result.
return results;
}
private static async Task<string> Task1()
{
await Task.Delay(3000);
return "Task1";
}
private static async Task<string> Task2()
{
await Task.Delay(1000);
return "Task2";
}
private static async Task<string> Task3()
{
await Task.Delay(5000);
return "Task3";
}
}
And to call it:
static void Main(string[] args)
{
var t = WhenAll.WaitForAllAsync();
t.ContinueWith((task) =>
{
string[] result = task.Result;
foreach(string taskname in result)
{
Console.WriteLine("Result: " + taskname);
}
});
t.Wait();
Console.ReadLine();
}
You can use ContinueWith method for every task that will update the progress.
So...
foreach (var t in tasks)
{
t.ContinueWith(Console.WriteLine("Hey I am done"));
}
1) Pass in a callback function
public async Task DoOperationsInParallelAsync(Action<Task<int>> completed)
{
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var tasks = new[] { DoOperation0(), DoOperation1(), DoOperation2() };
var completedTasks = tasks.Select(x => x.ContinueWith(completed, uiScheduler)).ToArray();
await Task.WhenAll(completedTasks);
}
private async void Button1_Click(object sender, EventArgs e)
{
await DoOperationsInParallelAsync(t => {
Label1.Text = string.Format("Task finished with {0}", t.Result);
});
}
2) The Task will run on the threadpool unless you specify it as long running or provide a TaskScheduler.
3) When only the first result matters (computation is redundant or time sensitive) such as grabbing stock prices from various providers mirroring similar data.