OSDN Git Service

サムネイル表示を中断するための CancellationTokenSource をTweenMain側に持たせる
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 25 Mar 2014 14:57:43 +0000 (23:57 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 26 Mar 2014 14:20:46 +0000 (23:20 +0900)
OpenTween.Tests/TweetThumbnailTest.cs
OpenTween/Tween.cs
OpenTween/TweetThumbnail.cs

index bd10473..a663f1c 100644 (file)
@@ -131,11 +131,12 @@ namespace OpenTween
             };
 
             using (var thumbbox = new TweetThumbnail())
+            using (var tokenSource = new CancellationTokenSource())
             {
                 SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
-                var task = thumbbox.ShowThumbnailAsync(post);
+                var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
 
-                thumbbox.CancelAsync();
+                tokenSource.Cancel();
 
                 await TestUtils.ThrowsAsync<TaskCanceledException>(async () => await task);
                 Assert.True(task.IsCanceled);
index c2fa945..01f0c4e 100644 (file)
@@ -6018,6 +6018,16 @@ namespace OpenTween
 
         private static PostClass displaypost = new PostClass();
 
+        /// <summary>
+        /// サムネイルの表示処理を表すタスク
+        /// </summary>
+        private Task thumbnailTask = null;
+
+        /// <summary>
+        /// サムネイル表示に使用する CancellationToken の生成元
+        /// </summary>
+        private CancellationTokenSource thumbnailTokenSource = null;
+
         private void DispSelectedPost(bool forceupdate)
         {
             if (_curList.SelectedIndices.Count == 0 || _curPost == null)
@@ -6167,7 +6177,19 @@ namespace OpenTween
                         this.SplitContainer3.Panel2Collapsed = true;
 
                         if (this.IsPreviewEnable)
-                            this.tweetThumbnail1.ShowThumbnailAsync(_curPost);
+                        {
+                            if (this.thumbnailTokenSource != null)
+                            {
+                                var oldTokenSource = this.thumbnailTokenSource;
+                                oldTokenSource.Cancel();
+                                this.thumbnailTask.ContinueWith(_ => oldTokenSource.Dispose());
+                            }
+
+                            this.thumbnailTokenSource = new CancellationTokenSource();
+
+                            var token = this.thumbnailTokenSource.Token;
+                            this.thumbnailTask = this.tweetThumbnail1.ShowThumbnailAsync(_curPost, token);
+                        }
                     }
                 }
                 catch (System.Runtime.InteropServices.COMException)
index 1b0a326..e3c838a 100644 (file)
@@ -39,9 +39,6 @@ namespace OpenTween
     {
         protected internal List<OTPictureBox> pictureBox = new List<OTPictureBox>();
 
-        private Task task = null;
-        private CancellationTokenSource cancelTokenSource;
-
         public event EventHandler ThumbnailLoading;
         public event EventHandler<ThumbnailDoubleClickEventArgs> ThumbnailDoubleClick;
         public event EventHandler<ThumbnailImageSearchEventArgs> ThumbnailImageSearchClick;
@@ -56,22 +53,14 @@ namespace OpenTween
         public TweetThumbnail()
         {
             InitializeComponent();
-
-            this.cancelTokenSource = new CancellationTokenSource();
         }
 
         public Task ShowThumbnailAsync(PostClass post)
         {
-            this.CancelAsync();
-
-            var cancelToken = this.cancelTokenSource.Token;
-
-            this.task = this.ShowThumbnailAsyncInternal(post, cancelToken);
-
-            return this.task;
+            return this.ShowThumbnailAsync(post, CancellationToken.None);
         }
 
-        private async Task ShowThumbnailAsyncInternal(PostClass post, CancellationToken cancelToken)
+        public async Task ShowThumbnailAsync(PostClass post, CancellationToken cancelToken)
         {
             var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
             var loadTasks = new List<Task>();
@@ -175,17 +164,6 @@ namespace OpenTween
             return ThumbnailGenerator.GetThumbnails(post);
         }
 
-        public void CancelAsync()
-        {
-            if (this.task == null || this.task.IsCompleted) return;
-
-            var oldTokenSource = this.cancelTokenSource;
-            this.cancelTokenSource = new CancellationTokenSource();
-
-            oldTokenSource.Cancel();
-            oldTokenSource.Dispose();
-        }
-
         /// <summary>
         /// 表示するサムネイルの数を設定する
         /// </summary>