Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuation not running C#

Consider the following code

class Program
{

    static void Continue()
    {
        Console.Out.WriteLine("Continue t1"); 
    }

    static async Task AsyncStart()
    {
        Console.Out.WriteLine("AsyncStart"); 
        return;
    }

    static void Main(string[] args)
    {

        Task t3 = AsyncStart().ContinueWith((ant) => { Continue(); });

        Console.Out.WriteLine("BEFORE"); 
        Task.WhenAny(new Task[] { t3 });
        Console.Out.WriteLine("AFTER");
     }
 }

The output is

AsyncStart
BEFORE
AFTER
Press any key to continue . . .

Continuation is not running!!!!

Yes, I understand the AsyncStart does not contain any await (CS1998), but I still expect t3 to run the continuation. I am missing something very basic

like image 858
Boris Avatar asked Jun 03 '26 18:06

Boris


1 Answers

Your process ended before the continuation (which runs on a background thread) had the chance to run.

like image 175
Matt Smith Avatar answered Jun 05 '26 07:06

Matt Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!