ContinueWith chaining not working as expected

Martin Sykora

I have this example code:

    static void Main(string[] args) {
        var t1 = Task.Run(async () => {
            Console.WriteLine("Putting in fake processing 1.");
            await Task.Delay(300);
            Console.WriteLine("Fake processing finished 1. ");
        });
        var t2 = t1.ContinueWith(async (c) => {
            Console.WriteLine("Putting in fake processing 2.");
            await Task.Delay(200);
            Console.WriteLine("Fake processing finished 2.");
        });
        var t3 = t2.ContinueWith(async (c)  => {
            Console.WriteLine("Putting in fake processing 3.");
            await Task.Delay(100);
            Console.WriteLine("Fake processing finished 3.");
        });
        Console.ReadLine();
    }

The console output baffles me:

  • Putting in fake processing 1.
  • Fake processing finished 1.
  • Putting in fake processing 2.
  • Putting in fake processing 3.
  • Fake processing finished 3.
  • Fake processing finished 2.

I am trying to chain the tasks so they execute one after another, what am I doing wrong? And I can't use await, this is just example code, in reality I am queueing incoming tasks (some asynchronous, some not) and want to execute them in the same order they came in but with no parallelism, ContinueWith seemed better than creating a ConcurrentQueue and handling everythning myself, but it just doesn't work...

Servy

Take a look at the type of t2. It's a Task<Task>. t2 will be completed when it finishes starting the task that does the actual work not when that work actually finishes.

The smallest change to your code to get it to work would be to add an unwrap after both your second and third calls to ContinueWith, so that you get out the task that represents the completion of your work.

The more idiomatic solution would be to simply remove the ContinueWith calls entirely and just use await to add continuations to tasks.

Interestingly enough, you would see the same behavior for t1 if you used Task.Factory.StartNew, but Task.Run is specifically designed to work with async lambdas and actually internally unwraps all Action<Task> delegates to return the result of the task returned, rather than a task that represents starting that task, which is why you don't need to unwrap that task.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optional Chaining Not Working As Expected

From Dev

C# Task ContinueWith Not Working as Expected

From Dev

Task.ContinueWith() is not executing as expected

From Dev

Task.ContinueWith() is not executing as expected

From Dev

Task ContinueWith does not return expected value

From Dev

.on("hover", ...) chaining not working

From Dev

Chaining not working on jQuery Plugin

From Dev

CABasicAnimation chaining not working

From Dev

chaining ifelse with mutate not working

From Dev

"NOT IN" not working as expected

From Dev

Method chaining using javascript not working?

From Dev

Method chaining using javascript not working?

From Dev

Javascript method chaining working but convoluted

From Dev

$this jQuery chaining with .on click function is not working

From Dev

chaining .add and .submit does not work as expected in IE

From Dev

Generic type inference not working with method chaining?

From Dev

jquery chaining not working in my html with each function

From Dev

Optional chaining not working for optional protocol requirements

From Dev

Operational chaining not working properly in NodeJS TS

From Java

Typescript optional chaining error: Expression expected.ts(1109)

From Dev

AutoResizeTextView Not Working as Expected

From Dev

PHP mktime not working as expected

From Dev

stopPropagation of jquery not working as expected

From Dev

laravel middleware not working as expected

From Dev

malloc() in C not working as expected

From Dev

__construct function not working as expected

From Dev

Javascript substring not working as expected

From Dev

bootstrap grid not working as expected

From Dev

substr() is not working as expected

Related Related

HotTag

Archive