Darajan
Darajan

Reputation: 893

Lambda expression as parameter to be used as AsTask() callback

I'm using the BackgroundTransfer.DownloadOperation in a method and I want the caller to be able to pass a lambda expression to be called when progress is updated:

DownloadOperation dwo = await DownloadFile(fileInfo,file);

This works:

var progressCallback = new Progress<DownloadOperation>(DefaultProgressCallback);
await dwo.StartAsync().AsTask(progressCallback);

But how do I define a lambda parameter to be able to do this?

var progressCallback = lambdaFromMethodCaller;
await dwo.StartAsync().AsTask(progressCallback);

Upvotes: 2

Views: 446

Answers (1)

Paul Chen
Paul Chen

Reputation: 1881

The AsTask extension method you use does not contain a signature that accepts delegate/Func, so you can't do .AsTask(...=>...)

But you can use lambda in this line:

var progressCallback = new Progress<DownloadOperation>(...=>...);

Since the constructor of Progress<T> accepts an Action<T>

Upvotes: 3

Related Questions