C# async methods still hang UI

后端 未结 3 1583
独厮守ぢ
独厮守ぢ 2020-12-10 05:59

I have these two methods, that I want to run async to keep the UI responsive. However, it\'s still hanging the UI. Any suggestions?

async void DoScrape()
            


        
3条回答
  •  死守一世寂寞
    2020-12-10 06:10

    I suspect DownloadStringTaskAsync relies upon HttpWebRequest.BeginGetResponse at a lower level. This being the case, it is known that the setup for a webrequest is not fully asynchronous. Annoyingly (and frankly, stupidly) the DNS lookup phase of an asynchronous WebRequest is performed synchronously, and therefore blocks. I suspect this might be the issue you are observing.

    Reproduced below is a warning in the docs:

    The BeginGetResponse method requires some synchronous setup tasks to complete (DNS resolution, proxy detection, and TCP socket connection, for example) before this method becomes asynchronous. As a result, this method should never be called on a user interface (UI) thread because it might take some time, typically several seconds. In some environments where the webproxy scripts are not configured properly, this can take 60 seconds or more. The default value for the downloadTime attribute on the config file element is one minute which accounts for most of the potential time delay.

    You've two choices:

    1. Start the request from a worker thread (and under high load, run the risk of ThreadPool starvation due to blocking behaviour)
    2. (Tenuously) Perform a programmatic DNS lookup prior to firing the request. This can be done asynchronously. Hopefully the request will then use the cached DNS lookup.

    We went for the 3rd (and costly) option of implementing our own properly asynchronous HTTP library to get decent throughput, but it's probably a bit extreme in your case ;)

提交回复
热议问题