Add new internal APIs for UIThread model (#4346)
authorhjhun <36876573+hjhun@users.noreply.github.com>
Tue, 14 Jun 2022 02:41:38 +0000 (11:41 +0900)
committerGitHub <noreply@github.com>
Tue, 14 Jun 2022 02:41:38 +0000 (11:41 +0900)
* Add Post() methods for getting the result

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
* Fix wrong implementation

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/Tizen.Applications.Common/Tizen.Applications/CoreApplication.cs
src/Tizen.Applications.Common/Tizen.Applications/CoreTask.cs

index 359cce0..26b8284 100644 (file)
@@ -21,6 +21,7 @@ using System.Globalization;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading;
+using System.Threading.Tasks;
 using System.Timers;
 using Tizen.Applications.CoreBackend;
 
@@ -305,6 +306,27 @@ namespace Tizen.Applications
         }
 
         /// <summary>
+        /// Dispatches an asynchronous message to the main loop of the CoreTask.
+        /// </summary>
+        /// <typeparam name="T">The type of the result.</typeparam>
+        /// <param name="runner">The runner callback.</param>
+        /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
+        /// <returns>A task with the result.</returns>
+        /// <since_tizen> 10 </since_tizen>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public async Task<T> Post<T>(Func<T> runner)
+        {
+            if (runner == null)
+            {
+                throw new ArgumentNullException(nameof(runner));
+            }
+
+            var task = new TaskCompletionSource<T>();
+            GSourceManager.Post(() => { task.SetResult(runner()); });
+            return await task.Task.ConfigureAwait(false);
+        }
+
+        /// <summary>
         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
         /// </summary>
         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
index 5a343f1..f675db3 100755 (executable)
  */
 
 using System;
-using System.Collections.Concurrent;
 using System.ComponentModel;
-using System.Threading;
-using static Tizen.Applications.TizenSynchronizationContext;
+using System.Threading.Tasks;
 
 namespace Tizen.Applications
 {
@@ -120,7 +118,7 @@ namespace Tizen.Applications
         /// Dispatches an asynchronous message to the main loop of the CoreApplication.
         /// </summary>
         /// <param name="runner">The runner callback.</param>
-        /// /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
+        /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
         /// <since_tizen> 10 </since_tizen>
         public void Post(Action runner)
         {
@@ -131,5 +129,26 @@ namespace Tizen.Applications
 
             GSourceManager.Post(runner, true);
         }
+
+        /// <summary>
+        /// Dispatches an asynchronous message to the main loop of the CoreApplication.
+        /// </summary>
+        /// <typeparam name="T">The type of the result. </typeparam>
+        /// <param name="runner">The runner callback.</param>
+        /// <exception cref="ArgumentNullException">Thrown when the runner is null.</exception>
+        /// <returns>A task with the result.</returns>
+        /// <since_tizen> 10 </since_tizen>
+
+        public async Task<T> Post<T>(Func<T> runner)
+        {
+            if (runner == null)
+            {
+                throw new ArgumentNullException(nameof(runner));
+            }
+
+            var task = new TaskCompletionSource<T>();
+            GSourceManager.Post(() => { task.SetResult(runner()); }, true);
+            return await task.Task.ConfigureAwait(false);
+        }
     }
 }