DEV Community

Discussion on: Using Async, Await, Plus a Slight Delay?

Collapse
 
joaofbantunes profile image
João Antunes

I guess it's a valid solution if you have no way to know when the PDF is available. The only problem is if for some unexpected reason it takes longer for the PDF to be available.

Maybe something like this could be useful?

var request = new HttpRequestMessage(HttpMethod.Head, url);
var response = await httpClient.SendAsync(request, cancellationToken);
//maybe limit the number of times this can be done
while (response.StatusCode != HttpStatusCode.OK)
{
    await Task.Delay(500, cancellationToken);
    response = await httpClient.SendAsync(request, cancellationToken);
}

It's basically polling the PDF url to check when it's available.

On a side note, how's that CancellationTokenSource being used? You're using it to cancel the execution given some event?

Collapse
 
catriname profile image
catrina

not currently, i cleaned it up! thanks for spotting it!