0

Please see the code excerpt below, which I found here: https://msdn.microsoft.com/en-us/library/dd537612(v=vs.110).aspx:

static void SimpleContinuation()
      {
         string path = @"C:\users\public\TPLTestFolder\";
         try
         {
            var firstTask = new Task(() => CopyDataIntoTempFolder(path));
            var secondTask = firstTask.ContinueWith((t) => CreateSummaryFile(path));
            firstTask.Start();
         }
         catch (AggregateException e)
         {
            Console.WriteLine(e.Message);
         }
      }

I am confused by the Lambda expression:

var secondTask = firstTask.ContinueWith((t) => CreateSummaryFile(path));

What is the purpose of: (t)? and why is it contained in brackets? t is not defined anywhere.

I have read this webpage, however it has not answered my question: https://msdn.microsoft.com/en-us/library/bb397687.aspx

3
  • It's passing the task to the continuation, which requires it (it's defined as an Action<Task>>) but doesn't use it. Commented Dec 20, 2016 at 11:25
  • The lambda requires that the signature includes a parameter. That doesn't mean it has to use it in the body. Commented Dec 20, 2016 at 11:34
  • "Why is it contained in brackets?" It doesn't have to be. Maybe it's done to look consistent or as a coding convention. "t is not defined anywhere" It is defined right here. Commented Dec 20, 2016 at 11:37

3 Answers 3

1

Task.ContinueWith needs an Action<Task> - a "method" that accepts a Task as its argument. t will be the Task argument that ContinueWith will supply that method with.

That method being something like:

void noName(Task t)
{
    CreateSummaryFile(path);
}

ContinueWith will then execute this anonymous method at the appropriate time supplying it with t (which will be ignored), and calling CreateSummaryFile(path).

Sign up to request clarification or add additional context in comments.

Comments

1

It would perhaps be a bit clearer if you realise that the form used there is a shorthand and the full form is:

(Task t) => CreateSummaryFile(path)

It defines an Action<Task> and so the counter to t is not defined anywhere is that it is in fact defined there.

You're allowed to leave out the type when it can be inferred from context (as it is here). You're also allowed to leave out the parentheses when there is exactly one parameter, so this could also be:

t => CreateSummaryFile(path)

Now, t isn't used here, but it often is and that can be very useful. More so when ContinueWith is called on a Task<TResult> as then the type of t would be Task<TResult> and the result could be used in the subsequent task.

Some people like to use _ for parameters that aren't used to signal "I have to put a parameter here, but it's not doing anything. But then some people hate that convention too. YMMV.

2 Comments

Thanks. If there is a function like this: Foo(int t), then the caller has to provide the value of 't'. In the case of continues with: t is not defined.
Ah. We would normally say that Foo defines t, not the caller. The caller of course passes a value, and similarly here the implementation of ContinueWith passes in the value, which will be the Task that has just completed. You can see this by e.g. experimenting with writing properties of the task to the console.
0

The signature of ContinueWith is:

Task ContinueWith(Action<Task> continuationAction)

The Action<Task> part means that you have to pass a delegate with one input parameter of type Task and a return of void. That is, ContinueWith will call that delegate at an appropriate time and pass an argument of type Task.

A lamdba expression in C# is a shorthand for a delegate. To the left of the lambda operator ( => ) are the parameters, to the right the body that will be executed. Since you will get an argument of type Task, you will need a parameter of that type. In your case that is t. That you don't need this value makes no difference. You still need to declare it to satisfy the lamdba expression's signature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.