OSDN Git Service

ThumbnailInfo.LoadThumbnailImage() をキャンセル可能なように変更
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 Jan 2014 09:21:11 +0000 (18:21 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 15 Jan 2014 12:32:32 +0000 (21:32 +0900)
OpenTween.Tests/TweetThumbnailTest.cs
OpenTween/Thumbnail/Services/Pixiv.cs
OpenTween/Thumbnail/Services/TonTwitterCom.cs
OpenTween/Thumbnail/ThumbnailInfo.cs
OpenTween/TweetThumbnail.cs

index 663f09c..cb106f5 100644 (file)
@@ -68,9 +68,9 @@ namespace OpenTween
 
             class MockThumbnailInfo : ThumbnailInfo
             {
-                protected override Task<MemoryImage> LoadThumbnailImageAsync()
+                public override Task<MemoryImage> 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);
                 }
             }
         }
index 6e59a9e..98951e2 100644 (file)
@@ -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<MemoryImage> LoadThumbnailImageAsync()
+            public override Task<MemoryImage> 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());
 
index eeb5003..e70cba2 100644 (file)
@@ -59,7 +59,7 @@ namespace OpenTween.Thumbnail.Services
 
         public class Thumbnail : ThumbnailInfo
         {
-            protected override Task<MemoryImage> LoadThumbnailImageAsync()
+            public override Task<MemoryImage> 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 スレッド上で実行されてしまう
             }
         }
     }
index 1edf7bd..eb41c65 100644 (file)
@@ -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<Task<MemoryImage>> ThumbnailImageTask;
-
-        public ThumbnailInfo()
+        public Task<MemoryImage> LoadThumbnailImageAsync()
         {
-            this.ThumbnailImageTask = new Lazy<Task<MemoryImage>>(this.LoadThumbnailImageAsync);
+            return this.LoadThumbnailImageAsync(CancellationToken.None);
         }
 
-        protected virtual Task<MemoryImage> LoadThumbnailImageAsync()
+        public virtual Task<MemoryImage> 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());
 
index f331a72..a88f22f 100644 (file)
@@ -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);