0

From the Microsoft APIs and reference documentation, I can read:

public Task<TNewResult> ContinueWith<TNewResult>(
    Func<Task<TResult>, TNewResult> continuationFunction
)

Parameters

continuationFunction

Type: System.Func<Task<TResult>, TNewResult>

A function to run when the Task completes. When run, the delegate will be passed the completed task as an argument.

Can't figure out, by which mechanism the Parameters continuationFunction refers to the Task.

For example:

    public  static Task<DateTimeOffset?> GetLastModified()
    {
        HttpClient theRequest = new HttpClient();

        var theTask = theRequest.GetAsync("https://www.yahoo.com/news/rss");

        return theTask.ContinueWith((Task<HttpResponseMessage> antecedent) => {
            return antecedent.Result.Content.Headers.LastModified;
        });
    }

How antecedent is linked with theTask?

  • In your case `antecedent` is `theTask` - simply it's referred within ContinueWith as a continuation function parameter. – Bartosz Sypytkowski Oct 23 '17 at 08:16
  • not really related to your particularly question; but take a look at `Result` https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions/Result.cs. You can chain your methods, what looks like to be the same as this, but then more readable ;) – Roelant M Oct 23 '17 at 08:16
  • @Horusiath I understand this, but how it works exactly? Where can see this in .net ? –  Oct 23 '17 at 08:24
  • Why do you need to know? Also, you should be using [await](https://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await) instead of using low-level API like ContinueWith. – Euphoric Oct 23 '17 at 08:28
  • @RoelantM better not. When talking about Tasks or asynchronous programming, .Result is Task.Result. And this isn't a question about functional programming or railroad-oriented programming anyway. That link will only cause confusion – Panagiotis Kanavos Oct 23 '17 at 08:31
  • @Euphoric, Good coder should always know how things work! –  Oct 23 '17 at 08:31
  • @Old.Man the read the docs? Or the source? `ContinueWith` or the equivalent `async/await` are core CLR functions. Their functionality is described in a *lot* of articles, blog posts and books. I recommend [Stephen Toub](https://blogs.msdn.microsoft.com/pfxteam/)'s and [Stephen Cleary](https://blog.stephencleary.com/)'s blogs and books. Stephen Toub wrote dozens of articles that explain the internals and how they can be combined. – Panagiotis Kanavos Oct 23 '17 at 08:33
  • @Old.Man this code could be rewritten as `var response= await client.GetAsync(...); return response.Content.Headers.LastModified;` btw. `async/await` were added in 2012 to make continuations (a lot) easier – Panagiotis Kanavos Oct 23 '17 at 08:34
  • While knowing what you use is nice idea, it becomes impossible as tools you use grow in number and complexity. Being shielded from implementation details of an API is why we are able to create such complex systems. It is good way to manage complexity. – Euphoric Oct 23 '17 at 08:56
  • @Panagiotis Kanavos: I have already read things from Stephen Cleary's blogs, for example this article: [link](https://blog.stephencleary.com/2015/01/a-tour-of-task-part-7-continuations.html) but cannot find any explanation that meet my need. And do not understand your questions "the read the docs? Or the source?" –  Oct 23 '17 at 09:25
  • @Euphoric: It is my operating mode (of course, as in my everyday life). And moreover, I come from C, where we have to deal with a lot of low level things. So I will not change anytime soon. –  Oct 23 '17 at 09:25
  • @Old.Man the lambda you pass to `ContinueWith` is a ... lambda, similar to C++. The Task is a `future`. In C++ you *don't* typically ask how the STL works, although you can [check the source](https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,3896). The methods are links themselves, so you can dive into [ContinueWithCore](https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,0a28c847d772a11b) and lower – Panagiotis Kanavos Oct 23 '17 at 10:16
  • @Old.Man if you continue down the tree, you'll find that [continuations are added to a queue](https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,8f3394185cf205ac) and called when the task completes. Again, that's implementation country that can and *does* change from one runtime version to another. – Panagiotis Kanavos Oct 23 '17 at 10:21
  • @PanagiotisKanavos: Thanks a lot, it exactly what I needed! –  Oct 23 '17 at 10:57

0 Answers0