I have code like this
public async Task DoStuff()
{
_client.Ready += () => client_Ready("test");
await Task.Delay(-1);
}
private async Task client_Ready(string message)
{
await _client.SendMessageAsync(message);
}
how can I say that after client_Ready
being invoked once the whole DoStuff
has to end/perform return instead of waiting?
Is there a "nice" way of doing it or I have to do some work around like throwing an Exception and catching it "above"?
MRE:
using System;
using System.Threading.Tasks;
public class Program
{
public static async Task RandomlyDelayedStuff()
{
Console.WriteLine("this ended");
await Task.Delay(200);
}
public static async Task DoStuff()
{
var rnd = new Random();
await Task.Delay(rnd.Next(0, 5000)).ContinueWith(async task =>
{
await RandomlyDelayedStuff();
});
await Task.Delay(-1);
}
public static async Task Main()
{
await DoStuff();
Console.WriteLine("ended");
}
}