};
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);
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)
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)
{
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;
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>();
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>