예제 #1
0
        public static async Task RunOnUiThread(this DispatchedHandler agileCallback)
        {
            CoreApplicationView view = CoreApplication.MainView;;

            if (view == null)
            {
                try
                {
                    view = CoreApplication.GetCurrentView();
                }
                catch
                {
                    view = null;
                }
            }

            if (view != null)
            {
                if (view.CoreWindow != null)
                {
                    if (view.CoreWindow.Dispatcher != null)
                    {
                        await view.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, agileCallback);
                    }
                    else
                    {
                        // ***
                        // *** Run on the current thread since there is no dispatcher.
                        // ***
                        agileCallback.Invoke();
                    }
                }
                else
                {
                    // ***
                    // *** Run on the current thread since there is no core window.
                    // ***
                    agileCallback.Invoke();
                }
            }
            else
            {
                // ***
                // *** Run on the current thread since there is no current view.
                // ***
                agileCallback.Invoke();
            }
        }
예제 #2
0
 public async static void RunActionOnUiThread(DispatchedHandler action)
 {
     if (dispatcher != null)
         await dispatcher.RunAsync(CoreDispatcherPriority.Normal, action);
     else
         action.Invoke();
 }
 /// <summary>
 /// Invokes the stored process.
 /// </summary>
 public void Invoke()
 {
     ReturnData = handler.Invoke();
     if (IsAttended)
     {
         resetEvent.Set();
     }
 }
예제 #4
0
 public static void RunInUI(DispatchedHandler act)
 {
     var disp = CoreApplication.MainView.Dispatcher;
     if (disp.HasThreadAccess)
         act.Invoke();
     else
         disp.RunAsync(CoreDispatcherPriority.Normal, act);
 }
예제 #5
0
 /// <summary>
 /// Runs action on UI thread.
 /// </summary>
 /// <param name="onUiThreadDelegate">
 /// The UI thread delegate.
 /// </param>
 /// <returns>
 /// The <see cref="IAsyncAction"/>.
 /// </returns>
 public static IAsyncAction ApplicationViewAsync(DispatchedHandler onUiThreadDelegate)
 {
     return(CoreApplication.MainView.CoreWindow
            .Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
     {
         onUiThreadDelegate?.Invoke();
     }));
 }
예제 #6
0
 /// <summary>
 /// Runs action on UI thread.
 /// </summary>
 /// <param name="onUiThreadDelegate">
 /// The UI thread delegate.
 /// </param>
 /// <returns>
 /// The <see cref="Task"/>.
 /// </returns>
 public static IAsyncAction CurrentViewAsync(DispatchedHandler onUiThreadDelegate)
 {
     return(CoreApplication.GetCurrentView()
            .Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
     {
         onUiThreadDelegate?.Invoke();
     }));
 }
예제 #7
0
 // Insert Dispatch below here
 protected async Task DispatchAsync(DispatchedHandler callback)
 {
     if (Dispatcher.HasThreadAccess)
     {
         callback.Invoke();
     }
     else
     {
         await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, callback);
     }
 }
예제 #8
0
 public async static void call(DispatchedHandler fun)
 {
     if (Window.Current.Dispatcher.HasThreadAccess)
     {
         fun.Invoke();
     }
     else
     {
         await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, fun);
     }
 }
예제 #9
0
        /// <summary>
        /// Runs the event dispatcher on UI thread
        /// </summary>
        /// <param name="coreDispatcherPriority"></param>
        /// <param name="agileCallBack"></param>
        public async static Task RunAsync(CoreDispatcherPriority coreDispatcherPriority, DispatchedHandler agileCallBack)
        {
            //If there is no dispatcher
            if (Dispatcher == null)
                Dispatcher = Window.Current.Dispatcher;

            //If it's already on UI Thread
            if (Dispatcher.HasThreadAccess)
                agileCallBack.Invoke();
            else
                await DispatcherHelper.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, agileCallBack);
        }
예제 #10
0
        public static void RunInUI(DispatchedHandler act)
        {
            var disp = CoreApplication.MainView.Dispatcher;

            if (disp.HasThreadAccess)
            {
                act.Invoke();
            }
            else
            {
                disp.RunAsync(CoreDispatcherPriority.Normal, act);
            }
        }
예제 #11
0
        public static Task ExecuteAsync(this CoreDispatcher dispatcher, DispatchedHandler action, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
        {
            if (null == dispatcher)
            {
                throw new Exception();
            }

            if (dispatcher.HasThreadAccess)
            {
                action.Invoke();
                return(Task.FromResult(true));
            }

            return(dispatcher.RunAsync(priority, action).AsTask());
        }
        /// <summary>
        /// Dispatches the specified action to be performed on the main thread.
        /// The caller thread will wait until the action is completed.
        /// </summary>
        public static object Dispatch(DispatchedHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            // If called from unity thread, just call the handler straight away.
            if (Thread.CurrentThread.ManagedThreadId == Instance.unityThreadId)
            {
                return(handler.Invoke());
            }

            return(Instance.DispatchInternal(new DispatchedItem(handler, true)));
        }
        /// <summary>
        /// Dispatches the specified action to be performed on the main thread.
        /// The caller thread will not wait until the action is completed.
        /// </summary>
        public static void DispatchUnattended(DispatchedHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            // If called from unity thread, just call the handler straight away.
            if (Thread.CurrentThread.ManagedThreadId == Instance.unityThreadId)
            {
                handler.Invoke();
                return;
            }

            Instance.DispatchInternal(new DispatchedItem(handler, false));
        }
예제 #14
0
        public IAsyncAction RunAsync(DispatchedHandler handler)
        {
            try
            {
                if (CoreWindow != null && CoreWindow.Dispatcher != null)
                {
                    return(CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, handler));
                }
                else
                {
                    handler.Invoke();
                }
            }
            catch (Exception uiEx) { UIError(uiEx); }

            return(new EmptyAsyncAction());
        }
예제 #15
0
        /// <summary>
        /// Executes the specified action on the UI thread
        /// </summary>
        /// <param name="agileCallback">The action to execute</param>
        public static async void RunOnUIThread(DispatchedHandler agileCallback)
        {
            var currentWindow = CoreApplication.MainView;
            if (currentWindow == null)
            {
                return;
            }

            var dispatcher = currentWindow.Dispatcher;
            if (dispatcher == null)
            {
                return;
            }

            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                agileCallback.Invoke();
            });
        }
예제 #16
0
        public static Task RunOnUIThreadAsync(CoreDispatcherPriority priority, DispatchedHandler handler)
        {
            if (!DesignMode.DesignModeEnabled)
            {
                var dispatcher = Dispatcher;

                if (dispatcher.HasThreadAccess)
                {
                    handler.Invoke();
                }
                else
                {
                    var task = dispatcher.RunAsync(priority, () => { handler(); }).AsTask();

                    return task;
                }
            }

            return Task.FromResult<object>(null);
        }
예제 #17
0
        public static Task RunOnUIThreadAsync(CoreDispatcherPriority priority, DispatchedHandler handler)
        {
            if (!DesignMode.DesignModeEnabled)
            {
                var dispatcher = Dispatcher;

                if (dispatcher.HasThreadAccess)
                {
                    handler.Invoke();
                }
                else
                {
                    var task = dispatcher.RunAsync(priority, () => { handler(); }).AsTask();

                    return(task);
                }
            }

            return(Task.FromResult <object>(null));
        }
        // Insert Dispatch below here
        protected async Task DispatchAsync(DispatchedHandler callback)
        {
            // As WASM is currently single-threaded, and Dispatcher.HasThreadAccess always returns false for broader compatibility reasons
            // the following code ensures the local code always directly invokes the callback on WASM.
            var hasThreadAccess =
#if __WASM__
                true;
#else
                Dispatcher.HasThreadAccess;
#endif

            if (hasThreadAccess)
            {
                callback.Invoke();
            }
            else
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, callback);
            }
        }
예제 #19
0
        /// <summary>
        /// Executes the specified action on the UI thread
        /// </summary>
        /// <param name="agileCallback">The action to execute</param>
        public static async void RunOnUIThread(DispatchedHandler agileCallback)
        {
            var currentWindow = CoreApplication.MainView;

            if (currentWindow == null)
            {
                return;
            }

            var dispatcher = currentWindow.Dispatcher;

            if (dispatcher == null)
            {
                return;
            }

            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                agileCallback.Invoke();
            });
        }
예제 #20
0
        private async void OnCommunicationServicePacketReceived(SocketCommunicationService sender, PacketReceivedEventArgs args)
        {
            var packet = args.Packet;

            switch (packet.PacketType)
            {
            case PacketType.QueryProfileResponse:
            {
                var profile = (QueryProfileResponsePacket)args.Packet;

                var print = new DispatchedHandler(() =>
                    {
                        CommandWindow
                        .WriteLine(
                            String.Format("Whoami: ({0:D})\"{1}\"", profile.UserId, profile.UserName),
                            LogLevel.Information
                            );
                    });

                if (Dispatcher.HasThreadAccess)
                {
                    print.Invoke();
                }
                else
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, print);
                }
                break;
            }

            case PacketType.QueryMessageResponse:
            {
                break;
            }
            }
        }
예제 #21
0
 /// <summary>
 /// Runs action on UI thread.
 /// </summary>
 /// <param name="onUiThreadDelegate">
 /// The UI thread delegate.
 /// </param>
 /// <param name="runOnUi">
 /// The run On Ui.
 /// </param>
 /// <returns>
 /// The <see cref="IAsyncAction"/>.
 /// </returns>
 public static IAsyncAction ApplicationViewAsyncIfRequired(DispatchedHandler onUiThreadDelegate, bool runOnUi)
 {
     return(runOnUi ?
            ApplicationViewAsync(onUiThreadDelegate) :
            AsyncInfo.Run(token => Task.Run(() => onUiThreadDelegate?.Invoke(), token)));
 }
예제 #22
0
        public IAsyncAction RunAsync(DispatchedHandler handler)
        {
            try
            {
                if (CoreWindow != null && CoreWindow.Dispatcher != null)
                {
                    return CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, handler);
                }
                else
                {
                    handler.Invoke();
                }
            }
            catch (Exception uiEx) { UIError(uiEx); }

            return new EmptyAsyncAction();
        }
예제 #23
0
 public async static void call(DispatchedHandler fun) {
     if (Window.Current.Dispatcher.HasThreadAccess)
         fun.Invoke();
     else
         await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, fun);
 }