Programmers learn & share
0 votes
118 views

Problem :

Facing following ASP.Net error

an asynchronous module or handler completed while an asynchronous operation was still pending.

by (6.9k points)   | 118 views

1 Answer

0 votes

Solution :

The method named myWebClient.DownloadStringTaskAsync runs on a separate thread and also it is non-blocking. A possible solution is to  DownloadDataCompleted event handler for myWebClient and a SemaphoreSlim class field.

e.g.

private SemaphoreSlim signalDownloadComplete = new SemaphoreSlim(0, 1);
private bool isDownloading = false;

....

//Adding to DownloadAsync() method
myWebClient.DownloadDataCompleted += (s, e) => {
 isDownloading = false;
 signalDownloadComplete.Release();
}
isDownloading = true;

...

//Adding to block main calling method from returning until download is completed 
if (isDownloading)
{
   await signalDownloadComplete.WaitAsync();
}

This will resolve your issue

by (36.1k points)  
2,204 questions
2,602 answers
59 comments
241 users