Ejemplo n.º 1
0
        /// <summary>
        /// Runs a function in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
        /// </summary>
        public static Task <T> RunInMainThread <T> (Func <T> func)
        {
            var ts = new TaskCompletionSource <T> ();

            if (IsMainThread)
            {
                try {
                    ts.SetResult(func());
                } catch (Exception ex) {
                    ts.SetException(ex);
                }
            }
            else
            {
                MainSynchronizationContext.Post(state => {
                    var(fun, tcs) = (ValueTuple <Func <T>, TaskCompletionSource <T> >)state;
                    try {
                        tcs.SetResult(fun());
                    } catch (Exception ex) {
                        tcs.SetException(ex);
                    }
                }, (func, ts));
            }
            return(ts.Task);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs an action in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
        /// </summary>
        public static Task RunInMainThread(Action action)
        {
            var ts = new TaskCompletionSource <int> ();

            if (IsMainThread)
            {
                try {
                    action();
                    ts.SetResult(0);
                } catch (Exception ex) {
                    ts.SetException(ex);
                }
            }
            else
            {
                MainSynchronizationContext.Post(delegate {
                    try {
                        action();
                        ts.SetResult(0);
                    } catch (Exception ex) {
                        ts.SetException(ex);
                    }
                }, null);
            }
            return(ts.Task);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Runs an action in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
        /// </summary>
        public static Task RunInMainThread(Action action)
        {
            var ts = new TaskCompletionSource <int> ();

            if (IsMainThread)
            {
                try {
                    action();
                    ts.SetResult(0);
                } catch (Exception ex) {
                    ts.SetException(ex);
                }
            }
            else
            {
                MainSynchronizationContext.Post(state => {
                    var(act, tcs) = (ValueTuple <Action, TaskCompletionSource <int> >)state;
                    try {
                        act();
                        tcs.SetResult(0);
                    } catch (Exception ex) {
                        tcs.SetException(ex);
                    }
                }, (action, ts));
            }
            return(ts.Task);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Runs an action in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
 /// </summary>
 /// <remarks>This version of the method is useful when the operation to be executed in the main
 /// thread is asynchronous.</remarks>
 public static Task RunInMainThread(Func <Task> func)
 {
     if (IsMainThread)
     {
         return(func());
     }
     else
     {
         var ts = new TaskCompletionSource <int> ();
         MainSynchronizationContext.Post(delegate {
             try {
                 var t = func();
                 t.ContinueWith(ta => {
                     try {
                         ts.SetResult(0);
                     } catch (Exception ex) {
                         ts.SetException(ex);
                     }
                 });
             } catch (Exception ex) {
                 ts.SetException(ex);
             }
         }, null);
         return(ts.Task);
     }
 }
        /// <summary>
        /// Dispatches the view updates.
        /// </summary>
        /// <param name="batchId">The batch identifier.</param>
        internal void DispatchViewUpdates(int batchId)
        {
            var operations = _operations.Count == 0 ? null : _operations;

            if (operations != null)
            {
                _operations = new List <Action>();
            }

            var nonBatchedOperations = default(Action[]);

            lock (_nonBatchedGate)
            {
                if (_nonBatchedOperations.Count > 0)
                {
                    nonBatchedOperations = _nonBatchedOperations.ToArray();
                    _nonBatchedOperations.Clear();
                }
                else
                {
                    nonBatchedOperations = null;
                }
            }

            lock (_gate)
            {
                _batches.Add(() =>
                {
                    using (RNTracer.Trace(RNTracer.TRACE_TAG_REACT_BRIDGE, "DispatchUI")
                           .With("BatchId", batchId)
                           .Start())
                    {
                        if (nonBatchedOperations != null)
                        {
                            foreach (var operation in nonBatchedOperations)
                            {
                                operation();
                            }
                        }

                        if (operations != null)
                        {
                            foreach (var operation in operations)
                            {
                                operation();
                            }
                        }

                        _nativeViewHierarchyManager.ClearLayoutAnimation();
                    }
                });
            }

            //Running batches async
            MainSynchronizationContext.Post(FlushPendingBatches);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Runs an action in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
 /// </summary>
 /// <remarks>This version of the method is useful when the operation to be executed in the main
 /// thread is asynchronous.</remarks>
 public static Task <T> RunInMainThread <T> (Func <Task <T> > func)
 {
     if (IsMainThread)
     {
         return(func());
     }
     else
     {
         var ts = new TaskCompletionSource <T> ();
         MainSynchronizationContext.Post(async state => {
             try {
                 ts.SetResult(await func());
             } catch (Exception ex) {
                 ts.SetException(ex);
             }
         }, null);
         return(ts.Task);
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Runs an action in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
 /// </summary>
 /// <remarks>This version of the method is useful when the operation to be executed in the main
 /// thread is asynchronous.</remarks>
 public static Task RunInMainThread(Func <Task> func)
 {
     if (IsMainThread)
     {
         return(func());
     }
     else
     {
         var ts = new TaskCompletionSource <int> ();
         MainSynchronizationContext.Post(async state => {
             var(fun, tcs) = (ValueTuple <Func <Task>, TaskCompletionSource <int> >)state;
             try {
                 await fun();
                 tcs.SetResult(0);
             } catch (Exception ex) {
                 tcs.SetException(ex);
             }
         }, (func, ts));
         return(ts.Task);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Runs a function in the main thread (usually the UI thread). The method returns a task, so it can be awaited.
        /// </summary>
        public static Task <T> RunInMainThread <T> (Func <T> func)
        {
            var ts = new TaskCompletionSource <T> ();

            if (IsMainThread)
            {
                try {
                    ts.SetResult(func());
                } catch (Exception ex) {
                    ts.SetException(ex);
                }
            }
            else
            {
                MainSynchronizationContext.Post(delegate {
                    try {
                        ts.SetResult(func());
                    } catch (Exception ex) {
                        ts.SetException(ex);
                    }
                }, null);
            }
            return(ts.Task);
        }
Ejemplo n.º 9
0
 public static void RunOnDispatcher(Action action)
 {
     //Async call
     MainSynchronizationContext.Post(action);
 }