OSDN Git Service

UserStreamsからの更新時にInvokeを呼び出す箇所をInvokeAsyncに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 9 Dec 2015 12:39:03 +0000 (21:39 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 10 Dec 2015 16:44:24 +0000 (01:44 +0900)
WaitingDialog クラスで使用していた InvokeAsync メソッドを OTBaseForm クラスに移動し、
全てのフォームで利用できるようにした。

OpenTween/OTBaseForm.cs
OpenTween/Tween.cs
OpenTween/WaitingDialog.cs

index 9d52298..80e6777 100644 (file)
@@ -25,6 +25,8 @@ using System.ComponentModel;
 using System.Drawing;
 using System.Linq;
 using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 
 namespace OpenTween
@@ -50,10 +52,13 @@ 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) =>
             {
@@ -63,6 +68,26 @@ namespace OpenTween
             };
         }
 
+        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>
index 7599d1f..7855694 100644 (file)
@@ -12904,8 +12904,7 @@ namespace OpenTween
             {
                 if (InvokeRequired && !IsDisposed)
                 {
-                    Invoke((Action)(() => this.RefreshTimeline()));
-                    return;
+                    await this.InvokeAsync(() => this.RefreshTimeline());
                 }
             }
             catch (ObjectDisposedException)
index 5e9479d..ecd7ee9 100644 (file)
@@ -39,7 +39,6 @@ namespace OpenTween
     /// </remarks>
     public partial class WaitingDialog : OTBaseForm
     {
-        private readonly SynchronizationContext synchronizationContext;
         private readonly Lazy<CancellationTokenSource> cancellationTokenSource;
 
         private bool cancellationEnabled = false;
@@ -62,7 +61,6 @@ namespace OpenTween
         {
             this.InitializeComponent();
 
-            this.synchronizationContext = SynchronizationContext.Current;
             this.cancellationTokenSource = new Lazy<CancellationTokenSource>();
 
             this.Timeout = TimeSpan.FromMilliseconds(500);
@@ -125,21 +123,6 @@ namespace OpenTween
             });
         }
 
-        /// <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&lt;T&gt; に変換したいだけ</summary>
         private async Task<int> ConvertTaskWithValue(Task task)
         {