From 558960314aa065f328843f56871bb1fb27f46242 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Tue, 14 Jan 2014 18:21:11 +0900 Subject: [PATCH] =?utf8?q?ThumbnailInfo.LoadThumbnailImage()=20=E3=82=92?= =?utf8?q?=E3=82=AD=E3=83=A3=E3=83=B3=E3=82=BB=E3=83=AB=E5=8F=AF=E8=83=BD?= =?utf8?q?=E3=81=AA=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/TweetThumbnailTest.cs | 4 ++-- OpenTween/Thumbnail/Services/Pixiv.cs | 7 ++++--- OpenTween/Thumbnail/Services/TonTwitterCom.cs | 4 ++-- OpenTween/Thumbnail/ThumbnailInfo.cs | 13 ++++++------- OpenTween/TweetThumbnail.cs | 9 +++++++-- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/OpenTween.Tests/TweetThumbnailTest.cs b/OpenTween.Tests/TweetThumbnailTest.cs index 663f09c9..cb106f5c 100644 --- a/OpenTween.Tests/TweetThumbnailTest.cs +++ b/OpenTween.Tests/TweetThumbnailTest.cs @@ -68,9 +68,9 @@ namespace OpenTween class MockThumbnailInfo : ThumbnailInfo { - protected override Task LoadThumbnailImageAsync() + public override Task LoadThumbnailImageAsync(CancellationToken token) { - return Task.Factory.StartNew(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl))); + return Task.Factory.StartNew(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl)), token); } } } diff --git a/OpenTween/Thumbnail/Services/Pixiv.cs b/OpenTween/Thumbnail/Services/Pixiv.cs index 6e59a9ed..98951e23 100644 --- a/OpenTween/Thumbnail/Services/Pixiv.cs +++ b/OpenTween/Thumbnail/Services/Pixiv.cs @@ -25,6 +25,7 @@ using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; namespace OpenTween.Thumbnail.Services @@ -52,15 +53,15 @@ namespace OpenTween.Thumbnail.Services public class Thumbnail : ThumbnailInfo { - protected override Task LoadThumbnailImageAsync() + public override Task LoadThumbnailImageAsync(CancellationToken token) { var client = new OTWebClient(); client.UserAgent = MyCommon.GetUserAgentString(fakeMSIE: true); client.Headers[HttpRequestHeader.Referer] = this.ImageUrl; - var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl)) - .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result)); + var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl), token) + .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result), token); task.ContinueWith(_ => client.Dispose()); diff --git a/OpenTween/Thumbnail/Services/TonTwitterCom.cs b/OpenTween/Thumbnail/Services/TonTwitterCom.cs index eeb50032..e70cba20 100644 --- a/OpenTween/Thumbnail/Services/TonTwitterCom.cs +++ b/OpenTween/Thumbnail/Services/TonTwitterCom.cs @@ -59,7 +59,7 @@ namespace OpenTween.Thumbnail.Services public class Thumbnail : ThumbnailInfo { - protected override Task LoadThumbnailImageAsync() + public override Task LoadThumbnailImageAsync(CancellationToken token) { return Task.Factory.StartNew(() => { @@ -77,7 +77,7 @@ namespace OpenTween.Thumbnail.Services throw new WebException(statusCode.ToString(), WebExceptionStatus.ProtocolError); } }, - CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); // 明示しないと TaskScheduler.Current になり UI スレッド上で実行されてしまう + token, TaskCreationOptions.None, TaskScheduler.Default); // 明示しないと TaskScheduler.Current になり UI スレッド上で実行されてしまう } } } diff --git a/OpenTween/Thumbnail/ThumbnailInfo.cs b/OpenTween/Thumbnail/ThumbnailInfo.cs index 1edf7bdb..eb41c651 100644 --- a/OpenTween/Thumbnail/ThumbnailInfo.cs +++ b/OpenTween/Thumbnail/ThumbnailInfo.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace OpenTween.Thumbnail @@ -34,19 +35,17 @@ namespace OpenTween.Thumbnail public string TooltipText { get; set; } public string FullSizeImageUrl { get; set; } - public readonly Lazy> ThumbnailImageTask; - - public ThumbnailInfo() + public Task LoadThumbnailImageAsync() { - this.ThumbnailImageTask = new Lazy>(this.LoadThumbnailImageAsync); + return this.LoadThumbnailImageAsync(CancellationToken.None); } - protected virtual Task LoadThumbnailImageAsync() + public virtual Task LoadThumbnailImageAsync(CancellationToken token) { var client = new OTWebClient(); - var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl)) - .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result)); + var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl), token) + .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result), token); task.ContinueWith(_ => client.Dispose()); diff --git a/OpenTween/TweetThumbnail.cs b/OpenTween/TweetThumbnail.cs index f331a72c..a88f22f3 100644 --- a/OpenTween/TweetThumbnail.cs +++ b/OpenTween/TweetThumbnail.cs @@ -85,14 +85,19 @@ namespace OpenTween picbox.ContextMenu = CreateContextMenu(thumb); picbox.ShowInitialImage(); - thumb.ThumbnailImageTask.Value.ContinueWith(t2 => + + thumb.LoadThumbnailImageAsync(cancelToken) + .ContinueWith(t2 => { if (t2.IsFaulted) - { t2.Exception.Flatten().Handle(x => x is WebException || x is InvalidImageException); + + if (t2.IsFaulted || t2.IsCanceled) + { picbox.ShowErrorImage(); return; } + picbox.Image = t2.Result; }, CancellationToken.None, TaskContinuationOptions.AttachedToParent, uiScheduler); -- 2.11.0