async / await / task

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,
Is good for understanding. Does anyone have the complete example?

dotnet/docs


Is not working

I use a class for that.

await Task.WhenAll(eggsTask, baconTask, toastTask);
await Task.Any(eggsTask, baconTask, toastTask);
or with ManualResetEvent

namespace TaskThreadAwait01
{
class Program
{
// Asynchrone Programmierung in C#

public class BaseCoffee
{

}

public class PourCoffee : BaseCoffee
{

}

public class Toast
{

}

public class Eggs
{

}


public class Working
{
public async Task Test()
{
PourCoffee cup = new PourCoffee();

Console.WriteLine("coffee is ready");
var eggsTask = FryEggsAsync(12);
var toastTask = MakeToastWithButterAndJamAsync(20);

var allTasks = new List<Task> { toastTask };
while (allTasks.Any())
{
Task finished = await Task.WhenAny(allTasks);
if (finished == eggsTask)
{
Console.WriteLine("eggs are ready");
}
else if (finished == toastTask)
{
Console.WriteLine("toast is ready");
}
allTasks.Remove(finished);
}

//Juice oj = PourOJ();
Console.WriteLine("oj is ready");
Console.WriteLine("Breakfast is ready!");
}

async Task<Eggs> FryEggsAsync(int number)
{
Eggs myEggs = new Eggs();
await Task.Delay(number * 1000);
return myEggs;
}

async Task<Toast> ToastBreadAsync(int number)
{
Toast myToast = new Toast();
await Task.Delay(number * 1000);
return myToast;
}

async Task<Toast> MakeToastWithButterAndJamAsync(int number)
{
var toast = await ToastBreadAsync(number);

ApplyButter(toast);
//ApplyJam(toast);
return toast;
}

public void ApplyButter(Toast toast)
{
// await Task.Delay(number * 1000);
}
}

static void Main(string[] args)
{
Working t1 = new Working();
t1.Test().Wait();
}
}

Greetings Markus

Continue reading...
 

Similar threads

N
Replies
0
Views
102
neonash
N
J
Replies
0
Views
105
JuanC_LopGar97
J
B
Replies
0
Views
126
BenTam
B
Back
Top