Пример #1
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);
        }
Пример #2
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);
        }
Пример #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);
        }
Пример #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);
     }
 }
 //TODO: Use log to instead temporarily,
 //need to decide whether to show these failures on error window
 public void HandleException(Exception exception)
 {
     //await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
     //{
     //  ExceptionDispatchInfo.Capture(exception).Throw();
     //}, DispatcherPriority.Send).Task.ConfigureAwait(false);
     RNTracer.Error(ReactConstants.Tag, "[RN_EXCEPTION] DisabledDevSupportManager::HandleException:[" + exception.ToString() + "]");
     MainSynchronizationContext.Quit();
 }
        /// <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);
        }
        protected override void OnCreate()
        {
            //Log.Info(ReactConstants.Tag, "## ReactProgram ##  Enter Constructor ReactProgram()");
            try
            {
                _reactInstanceManager = CreateReactInstanceManager();
            }
            catch (Exception ex)
            {
                Log.Info(ReactConstants.Tag, "## ReactProgram ## CreateReactInstanceManager Ex:" + ex.ToString());
            }
            //Log.Info(ReactConstants.Tag, "## ReactProgram ##  Exit Constructor ReactProgram()");

            MainSynchronizationContext.Initialize(SynchronizationContext.Current, Exit);

            Elementary.Initialize();
            Elementary.ThemeOverlay();

            //Log.Info(ReactConstants.Tag, "## ReactProgram ##  Enter OnCreate()");

            ResourceDir = DirectoryInfo.Resource;

            // 1. Create root window
            ReactWindow rctWin = new ReactWindow("ElmSharp Window");

            RctWindow = rctWin;
            RctWindow.Show();
            RctWindow.BackButtonPressed += (object sender, EventArgs e) =>
            {
                Log.Debug(ReactConstants.Tag, "## Back button being Pressed ##");
                _reactInstanceManager.OnBackPressed();
            };

            RctWindow.RedButtonPressed += (object sender, EventArgs e) =>
            {
                Log.Debug(ReactConstants.Tag, "## Red button being Pressed ##");
                _reactInstanceManager.DevSupportManager.ShowDevOptionsDialog();
            };

            // 2. Create root view
            RootView = CreateRootView();
            //RootView.Show();


            // 3. Entry of 'JS' world
            RootView.StartReactApplication(_reactInstanceManager, MainComponentName);

            // 4. Set root view
            //RctWindow.Navigator.Push(RootView, "Instagram");
            RctWindow.SetMainPage(RootView);

            base.OnCreate();

            //Log.Info(ReactConstants.Tag, "## ReactProgram ##  Exit OnCreate()");
        }
Пример #8
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);
     }
 }
Пример #9
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);
     }
 }
Пример #10
0
 public DispatcherMessageQueueThread(string name, Action <Exception> handler)
     : base(name)
 {
     _actionSubject  = new Subject <Action>();
     _actionObserver = _actionSubject;
     _subscription   = _actionSubject
                       //Remove "ObserveOn"
                       //TODO: Need to check whether it can works well.
                       //.ObserveOn(Dispatcher.CurrentDispatcher)
                       .Subscribe(action =>
     {
         try
         {
             //Sync call
             MainSynchronizationContext.Send(action);
         }
         catch (Exception ex)
         {
             RNTracer.Error(ReactConstants.Tag, "[RN_EXCEPTION]At action:[" + ex.ToString() + "]");
             handler(ex);
         }
     });
 }
Пример #11
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);
        }
Пример #12
0
 public static void RunOnDispatcher(Action action)
 {
     //Async call
     MainSynchronizationContext.Post(action);
 }