は解決策である:ここでは
public async Task<Image> GetImageAsync(string url)
{
var tcs = new TaskCompletionSource<Image>();
Image webImage = null;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
.ContinueWith(task =>
{
var webResponse = (HttpWebResponse) task.Result;
Stream responseStream = webResponse.GetResponseStream();
if (webResponse.ContentEncoding.ToLower().Contains("gzip"))
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
else if (webResponse.ContentEncoding.ToLower().Contains("deflate"))
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
if (responseStream != null) webImage = Image.FromStream(responseStream);
tcs.TrySetResult(webImage);
webResponse.Close();
responseStream.Close();
});
return tcs.Task.Result;
}
は、上記の溶液を呼び出す方法です:
PictureBox img = new System.Windows.Forms.PictureBox();
var result = GetImageAsync(ThumbnailUrl);
result.ContinueWith(task =>
{
img.Image = task.Result;
});
時間:
それはと同等です。 –
ありがとうございました、私は今検索します – ara
Backgroundworkerの 'dowork'イベントではなく – Rahul