1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
24 using System.ComponentModel;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using System.Windows.Forms;
34 /// タスクが待機中であることを表示するダイアログ
37 /// 一定時間 (Timeout プロパティで指定する) 以上経ってもタスクが完了しない場合にダイアログを表示します。
38 /// EnableCancellation メソッドを使用することでキャンセル機能を提供することも可能です。
40 public partial class WaitingDialog : OTBaseForm
42 private readonly Lazy<CancellationTokenSource> cancellationTokenSource;
44 private bool cancellationEnabled = false;
47 /// ダイアログを表示せずに待機する最短の待ち時間 (デフォルト0.5秒)
49 public TimeSpan Timeout { get; set; }
56 get => this.labelMessage.Text;
57 set => this.labelMessage.Text = value;
60 public WaitingDialog()
62 this.InitializeComponent();
64 this.cancellationTokenSource = new Lazy<CancellationTokenSource>();
66 this.Timeout = TimeSpan.FromMilliseconds(500);
69 public WaitingDialog(string message)
72 this.Message = message;
76 /// キャンセル機能を有効にし、キャンセルを通知するための CancellationToken を返します
78 public CancellationToken EnableCancellation()
80 this.cancellationEnabled = true;
81 this.ControlBox = true;
83 var cts = this.cancellationTokenSource.Value;
87 public Task WaitForAsync(Task task)
88 => this.WaitForAsync(this.ConvertTaskWithValue(task));
90 public Task WaitForAsync(IWin32Window owner, Task task)
91 => this.WaitForAsync(owner, this.ConvertTaskWithValue(task));
93 public Task<T> WaitForAsync<T>(Task<T> task)
94 => this.WaitForAsync(null, task);
97 /// タスクを待機し、状況に応じて待機中ダイアログを表示します
99 /// <param name="owner">ダイアログのオーナー</param>
100 /// <param name="task">待機するタスク</param>
101 public Task<T> WaitForAsync<T>(IWin32Window owner, Task<T> task)
103 return Task.Run(async () =>
105 // 指定された秒数以内で完了すればダイアログは表示しない
106 var timeout = Task.Delay(this.Timeout);
107 if (await Task.WhenAny(task, timeout) != timeout)
110 var dialogTask = this.InvokeAsync(() => this.ShowDialog(owner));
112 // キャンセルされずにタスクが先に完了したらダイアログを閉じる
113 if (await Task.WhenAny(task, dialogTask) != dialogTask)
114 await this.InvokeAsync(() => this.DialogResult = DialogResult.OK);
120 /// <summary>Task を Task<T> に変換したいだけ</summary>
121 private async Task<int> ConvertTaskWithValue(Task task)
123 await task.ConfigureAwait(false);
127 private void ProgressDialog_FormClosing(object sender, FormClosingEventArgs e)
129 if (this.cancellationEnabled)
131 if (e.CloseReason == CloseReason.UserClosing)
133 var cts = this.cancellationTokenSource.Value;
139 private void ProgressDialog_KeyDown(object sender, KeyEventArgs e)
141 if (this.cancellationEnabled)
143 if (e.KeyCode == Keys.Escape)
145 var cts = this.cancellationTokenSource.Value;
148 this.DialogResult = DialogResult.Cancel;