Question

I have previously used the ActionBlock via another constructor:

ActionBlock<TInput> Constructor (Action<TInput>)

But for the one in the title, with a return type Task, I am not sure what the ActionBlock does with the returned Task. I presume that it is for somehow waiting on an async delegate supplied into the constructor? Can I get a hold of it?

Was it helpful?

Solution

OK I should have guessed that it's for supplying an asynchonous delegate. I must have been a bit unfamiliar with the syntax for that. Here is an example of one such delegate:

var writer = new ActionBlock<string>(async url =>
{
    WebClient wc = new WebClient();
    // using IOCP the thread pool worker thread does return to the pool
    byte[] buffer = await wc.DownloadDataTaskAsync(url);
    string fileName = Path.GetFileName(url);

    string name = @"Images\" + fileName;

    using (Stream srm = File.OpenWrite(name))
    {
        await srm.WriteAsync(buffer, 0, buffer.Length);
    }
});

So that delegate async url => can be said to have the type Func<String, Task>.

The example is from: http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/28/tpl-dataflow-walkthrough-part-5.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top