Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c# - How do DownloadStringTaskAsync and DownloadStringAsync manage to not block the UI thread?

As far as I know, they both do not have the "async" keyword. Do they both create a new thread when downloading? I've heard people say that they do not use a new thread. Then how does it do it?

The normal DownloadString method does indeed block the UI thread, but how do the other 2 methods manage to do it without using the "await" keyword to return from the method and wait until the operation is completed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as I know, they both do not have the "async" keyword. Do they both create a new thread when downloading?

True, they do not use async. But you don't have to use async in order to be asynchronous.

They do not create new threads, if by that you mean that they just queue the blocking work to the thread pool. They do "use thread resources" when the operation completes, though I would say that documentation is misleading.

I've heard people say that they do not use a new thread. Then how does it do it?

See There Is No Thread. In summary, true asynchronous I/O uses callbacks, which generally ends at a completion port, which in .NET is part of the thread pool. However, there is not a thread that is blocked doing the I/O; rather, the thread pool's I/O threads (not worker threads) are used to service the completion port work as it completes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...