using System.Drawing;
using System.Linq;
using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
using System.Windows.Forms;
namespace OpenTween
/// </remarks>
public SizeF CurrentScaleFactor { get; private set; }
+ private readonly SynchronizationContext synchronizationContext;
+
protected OTBaseForm()
: base()
{
this.CurrentScaleFactor = new SizeF(1.0f, 1.0f);
+ this.synchronizationContext = SynchronizationContext.Current;
this.Load += (o, e) =>
{
};
}
+ public Task InvokeAsync(Action x)
+ {
+ return this.InvokeAsync<object>(() => { x(); return null; });
+ }
+
+ /// <summary>
+ /// Control.InvokeメソッドのTask版みたいなやつ
+ /// </summary>
+ public Task<T> InvokeAsync<T>(Func<T> x)
+ {
+ var tcs = new TaskCompletionSource<T>();
+ this.synchronizationContext.Post(_ =>
+ {
+ var ret = x();
+ tcs.SetResult(ret);
+ }, null);
+
+ return tcs.Task;
+ }
+
/// <summary>
/// source で指定されたフォントのスタイルを維持しつつ GlobalFont に置き換えた Font を返します
/// </summary>
{
if (InvokeRequired && !IsDisposed)
{
- Invoke((Action)(() => this.RefreshTimeline()));
- return;
+ await this.InvokeAsync(() => this.RefreshTimeline());
}
}
catch (ObjectDisposedException)
/// </remarks>
public partial class WaitingDialog : OTBaseForm
{
- private readonly SynchronizationContext synchronizationContext;
private readonly Lazy<CancellationTokenSource> cancellationTokenSource;
private bool cancellationEnabled = false;
{
this.InitializeComponent();
- this.synchronizationContext = SynchronizationContext.Current;
this.cancellationTokenSource = new Lazy<CancellationTokenSource>();
this.Timeout = TimeSpan.FromMilliseconds(500);
});
}
- /// <summary>
- /// Control.InvokeメソッドのTask版みたいなやつ
- /// </summary>
- private Task<T> InvokeAsync<T>(Func<T> x)
- {
- var tcs = new TaskCompletionSource<T>();
- this.synchronizationContext.Post(_ =>
- {
- var ret = x();
- tcs.SetResult(ret);
- }, null);
-
- return tcs.Task;
- }
-
/// <summary>Task を Task<T> に変換したいだけ</summary>
private async Task<int> ConvertTaskWithValue(Task task)
{