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.
25 using System.Collections.Generic;
26 using System.ComponentModel;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
36 /// タスクが待機中であることを表示するダイアログ
39 /// 一定時間 (Timeout プロパティで指定する) 以上経ってもタスクが完了しない場合にダイアログを表示します。
40 /// EnableCancellation メソッドを使用することでキャンセル機能を提供することも可能です。
42 public partial class WaitingDialog : OTBaseForm
44 private readonly Lazy<CancellationTokenSource> cancellationTokenSource;
46 private bool cancellationEnabled = false;
49 /// ダイアログを表示せずに待機する最短の待ち時間 (デフォルト0.5秒)
51 public TimeSpan Timeout { get; set; }
58 get => this.labelMessage.Text;
59 set => this.labelMessage.Text = value;
62 public WaitingDialog()
64 this.InitializeComponent();
66 this.cancellationTokenSource = new Lazy<CancellationTokenSource>();
68 this.Timeout = TimeSpan.FromMilliseconds(500);
71 public WaitingDialog(string message)
74 this.Message = message;
78 /// キャンセル機能を有効にし、キャンセルを通知するための CancellationToken を返します
80 public CancellationToken EnableCancellation()
82 this.cancellationEnabled = true;
83 this.ControlBox = true;
85 var cts = this.cancellationTokenSource.Value;
89 public Task WaitForAsync(Task task)
90 => this.WaitForAsync(this.ConvertTaskWithValue(task));
92 public Task WaitForAsync(IWin32Window? owner, Task task)
93 => this.WaitForAsync(owner, this.ConvertTaskWithValue(task));
95 public Task<T> WaitForAsync<T>(Task<T> task)
96 => this.WaitForAsync(null, task);
99 /// タスクを待機し、状況に応じて待機中ダイアログを表示します
101 /// <param name="owner">ダイアログのオーナー</param>
102 /// <param name="task">待機するタスク</param>
103 public Task<T> WaitForAsync<T>(IWin32Window? owner, Task<T> task)
105 return Task.Run(async () =>
107 // 指定された秒数以内で完了すればダイアログは表示しない
108 var timeout = Task.Delay(this.Timeout);
109 if (await Task.WhenAny(task, timeout) != timeout)
112 var dialogTask = this.InvokeAsync(() => this.ShowDialog(owner));
114 // キャンセルされずにタスクが先に完了したらダイアログを閉じる
115 if (await Task.WhenAny(task, dialogTask) != dialogTask)
116 await this.InvokeAsync(() => this.DialogResult = DialogResult.OK);
122 /// <summary>Task を Task<T> に変換したいだけ</summary>
123 private async Task<int> ConvertTaskWithValue(Task task)
125 await task.ConfigureAwait(false);
129 private void ProgressDialog_FormClosing(object sender, FormClosingEventArgs e)
131 if (this.cancellationEnabled)
133 if (e.CloseReason == CloseReason.UserClosing)
135 var cts = this.cancellationTokenSource.Value;
141 private void ProgressDialog_KeyDown(object sender, KeyEventArgs e)
143 if (this.cancellationEnabled)
145 if (e.KeyCode == Keys.Escape)
147 var cts = this.cancellationTokenSource.Value;
150 this.DialogResult = DialogResult.Cancel;