我發現下面的例子來下載網頁(串)asynchounously:網上下載圖像異步在.net
let getImage (imageUrl:string) =
async {
try
let req = WebRequest.Create(imageUrl) :?> HttpWebRequest
req.UserAgent <- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
req.Method <- "GET";
req.AllowAutoRedirect <- true;
req.MaximumAutomaticRedirections <- 4;
let! response1 = req.AsyncGetResponse()
let response = response1 :?> HttpWebResponse
use stream = response.GetResponseStream()
use streamreader = new System.IO.StreamReader(stream)
return! streamreader.AsyncReadToEnd() // .ReadToEnd()
with
_ -> return "" // if there's any exception, just return an empty string
}
它返回一個字符串。但是,我需要以異步的方式下載在線圖像(字節數組)。
任何人都可以給我一些提示?
爲4.5+ '異步'/ '等待' 版本檢查 - http://stackoverflow.com/questions/11638821/c-sharp-net-4-5-async -多線程 – 2014-09-18 16:40:58