Example #1
1
 public static void BeginInvoke(this Control control, Action action)
 {
     if (control.InvokeRequired)
         control.BeginInvoke(action);
     else
         action();
 }
        public static void SafeInvoke(this Control ui_element,
            Action updater, bool force_synchronous)
        {
            if (ui_element == null)
                return;

            if (ui_element.InvokeRequired) {
                if (force_synchronous) {
                    ui_element.Invoke ((Action) delegate {
                        SafeInvoke (ui_element, updater, force_synchronous);
                    });

                } else {
                    ui_element.BeginInvoke ((Action) delegate {
                        SafeInvoke (ui_element, updater, force_synchronous);
                    });
                }

            } else {
                if (ui_element.IsDisposed)
                    throw new ObjectDisposedException ("Control is already disposed.");

                updater ();
            }
        }
Example #3
1
        public static void Invoke(this Control uiElement, Action updater, bool forceSynchronous = true)
        {
            if (uiElement == null)
            {
                throw new ArgumentNullException("uiElement");
            }

            if (uiElement.InvokeRequired)
            {
                if (forceSynchronous)
                {
                    try
                    {
                        uiElement.Invoke((Action)delegate { Invoke(uiElement, updater, forceSynchronous); });
                    }
                    catch (Exception e) { }
                }
                else
                {
                    uiElement.BeginInvoke((Action)delegate { Invoke(uiElement, updater, forceSynchronous); });
                }
            }
            else
            {
                if (!uiElement.IsDisposed)
                {
                    updater();
                }
            }
        }
 public static void Invoke(this UserControl c, Action a)
 {
     if (!c.InvokeRequired)
         a();
     else if (!c.IsDisposed && !c.Disposing && c.Created)
         c.BeginInvoke(a);
 }
Example #5
0
 /*
  * Executes the action asynchronously on the UI thread, without blocking the calling thread.
  */
 internal static void InvokeOnUiThreadIfRequired(this Control control, Action action) {
     if(control.InvokeRequired) {
         control.BeginInvoke(action);
     } else {
         action.Invoke();
     }
 }
        /// <summary>
        /// Executes the specified action asynchronously on the thread the Dispatcher is associated with, after the specified timeout.
        /// </summary>
        /// <param name="dispatcher">The dispatcher instance.</param>
        /// <param name="timeout">The <see cref="TimeSpan"/> representing the amount of time to delay before the action is invoked.</param>
        /// <param name="action">The <see cref="Action"/> to execute.</param>
        public static void BeginInvokeAfterTimeout(this Dispatcher dispatcher, TimeSpan timeout, Action action)
        {
            if (!dispatcher.CheckAccess())
            {
                dispatcher.BeginInvoke(() => dispatcher.BeginInvokeAfterTimeout(timeout, action));
            }
            else
            {
                var dispatcherTimer = new DispatcherTimer()
                {
                    Interval = timeout
                };

                dispatcherTimer.Tick += (s, e) =>
                {
                    dispatcherTimer.Stop();

                    dispatcherTimer = null;

                    action();
                };

                dispatcherTimer.Start();
            }
        }
Example #7
0
 public static void FireAndForget(this Action action, object state = null)
 {
     lock (_invokedActions)
     {
         _invokedActions.Add(action.BeginInvoke(Forget, state), action);
     }
 }
 /// <summary>
 /// A simple threading extension method, to invoke a delegate
 /// on the correct thread asynchronously if it is not currently 
 /// on the correct thread which can be used with DispatcherObject types.
 /// </summary>
 /// <param name="dispatcher">The Dispatcher object on which to 
 /// perform the Invoke</param>
 /// <param name="action">The delegate to run</param>
 public static void InvokeAsynchronouslyInBackground(this Dispatcher dispatcher, Action action)
 {
     if (dispatcher != null)
         dispatcher.BeginInvoke(DispatcherPriority.Background, action);
     else
         action();
 }
Example #9
0
 /// <summary>
 /// Функция упрощающая использование Invoke() для Windows Forms приложений
 /// </summary>
 /// <param name="ctrl">Элемент управления для которого необходимо вызвать Invoke()</param>
 /// <param name="cmd"></param>
 /// <example>myCtrl.SafeInvoke(() => myCtrl.Enabled = false);</example>
 public static void SafeInvoke(this Control ctrl, Action cmd)
 {
     if (ctrl.InvokeRequired)
         ctrl.BeginInvoke(cmd);
     else
         cmd();
 }
Example #10
0
 public static void PerformOnMainThread(this Control control, Action action)
 {
     if (control.InvokeRequired)
         control.BeginInvoke(action);
     else
         action();
 }
Example #11
0
 /// <summary>
 /// Set text to the control thread-safely.
 /// </summary>
 /// <param name="c"></param>
 /// <param name="m"></param>
 public static void SetText(this Control c, string m)
 {
     if (c.InvokeRequired)
         c.BeginInvoke(new MethodInvoker(() => { c.Text = m; }));
     else
         c.Text = m;
 }
 public static void adoptAsync(this Dispatcher dispatcher, Action del)
 {
     if(dispatcher.CheckAccess())
         del();
     else
         dispatcher.BeginInvoke(del);
 }
 public static void adoptAsync(this Dispatcher dispatcher, Action del)
 {
     if (Thread.CurrentThread == dispatcher.Thread)
         del();
     else
         dispatcher.BeginInvoke(del);
 }
Example #14
0
 public static void BeginInvoke(this Control c, MethodInvoker code)
 {
     //c.BeginInvoke(code);
     if (c.InvokeRequired)
         c.BeginInvoke(code);
     else
         c.Invoke(code);
 }
Example #15
0
File: Control.cs Project: qida/qcv
 public static void BeginInvokeIfRequired(this Control control, MethodInvoker action)
 {
     if (control.InvokeRequired) {
     control.BeginInvoke(action);
       } else {
     action();
       }
 }
Example #16
0
 public static void BeginInvoke(this Form window, Action action)
 {
     if (window.IsDisposed || !window.IsHandleCreated)
     {
         return;
     }
     window.BeginInvoke(new BeginInvokeDelegate(action));
 }
Example #17
0
 public static void InvokeOrExecute(this Dispatcher dispatcher, Action action)
 {
     if (dispatcher.CheckAccess()) {
         action();
     } else {
         dispatcher.BeginInvoke(DispatcherPriority.Normal, action);
     }
 }
Example #18
0
 /// <summary>
 /// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread.
 /// </summary>
 /// <param name="control"></param>
 /// <param name="code"></param>
 public static void UIThread(this Control @this, Action code)
 {
     if (@this.InvokeRequired) {
         @this.BeginInvoke(code);
     } else {
         code.Invoke();
     }
 }
 public static DispatcherOperation BeginInvoke(
     this IDispatcherService dispatcher,
     Action action,
     DispatcherPriority priority
     )
 {
     return dispatcher.BeginInvoke(action, priority);
 }
Example #20
0
 public static void DoEvents(this Dispatcher dispatcher)
 {
     var frame = new DispatcherFrame();
     dispatcher.BeginInvoke(
         DispatcherPriority.Background,
         new DispatcherOperationCallback(ExitFrame),
         frame);
     Dispatcher.PushFrame(frame);
 }
Example #21
0
 public static void UiThread(this Control control, Action action)
 {
     if (control.InvokeRequired)
     {
         control.BeginInvoke(action);
         return;
     }
     action.Invoke();
 }
Example #22
0
 public static void BeginInvokeIfRequired(this Form f, Action a)
 {
     if (f.InvokeRequired)
     {
         f.BeginInvoke(a);
         return;
     }
     a();
 }
 public static PageAsyncTask ToPageAsyncTask(this Func<CancellationToken, Task> func)
 {
     var cts = new CancellationTokenSource();
     return new PageAsyncTask(
         (sender, e, cb, extraData) => func.BeginInvoke(cts.Token, cb, extraData),
         ar => func.EndInvoke(ar).Wait(),
         ar => cts.Cancel(), 
         null);
 }
Example #24
0
 public static void UIThread(this Control control, Action code)
 {
     if (control.InvokeRequired)
     {
         control.BeginInvoke(code);
         return;
     }
     code.Invoke();
 }
Example #25
0
        private static void Do(this Dispatcher dispatcher, Action action)
        {
            if (!dispatcher.CheckAccess())
            {
                dispatcher.BeginInvoke(action, DispatcherPriority.Background);
                return;
            }

            action();
        }
Example #26
0
        /// <summary>
        /// Executes the specified action with the specified arguments synchronously on the thread the Dispatcher is associated with.
        /// </summary>
        /// <param name="dispatcher">The dispatcher.</param>
        /// <param name="action">The action.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception>
        /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c>
        /// method will be used instead.</remarks>
        public static void Invoke(this Dispatcher dispatcher, Action action)
        {
            Argument.IsNotNull("action", action);

            if (dispatcher != null && !dispatcher.CheckAccess())
            {
#if NET
                dispatcher.Invoke(action, null);
#elif NETFX_CORE
                dispatcher.BeginInvoke(action);
#else
                dispatcher.BeginInvoke(action);
#endif
            }
            else
            {
                action.Invoke();
            }
        }
Example #27
0
 /// <summary>
 /// Run all outstanding events queued on the provided Dispatcher
 /// </summary>
 /// <param name="dispatcher"></param>
 public static void DoEvents(this Dispatcher dispatcher)
 {
     var frame = new DispatcherFrame();
     Action<DispatcherFrame> action = _ => { frame.Continue = false; };
     dispatcher.BeginInvoke(
         DispatcherPriority.SystemIdle,
         action,
         frame);
     Dispatcher.PushFrame(frame);
 }
		public static void Invoke(this Forms.Control me, Action action)
		{
			if (me.InvokeRequired)
			{
				try { me.BeginInvoke((Delegate)action); }
				catch (System.InvalidOperationException) { }
			}
			else
				action();
		}
        /// <summary>
        /// Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with.
        /// </summary>
        /// <param name="dispatcher">The dispatcher.</param>
        /// <param name="method">A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.</param>
        /// <param name="args">An array of objects to pass as arguments to the given method. Can be <c>null</c>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="method" /> is <c>null</c>.</exception>
        /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c>
        /// method will be used instead.</remarks>
        public static void Invoke(this Dispatcher dispatcher, Delegate method, params object[] args)
        {
            Argument.IsNotNull("method", method);

            if (dispatcher != null && !dispatcher.CheckAccess())
            {
#if NET
                dispatcher.Invoke(method, args);
#elif NETFX_CORE
                dispatcher.BeginInvoke(() => method.DynamicInvoke(args));
#else
                dispatcher.BeginInvoke(method, args);
#endif
            }
            else
            {
                method.DynamicInvoke(args);
            }
        }
 public static async Task InvokeAsync(this Dispatcher source, Action action)
 {
     TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
     source.BeginInvoke(() =>
     {
         action();
         tcs.SetResult(null);
     });
     await tcs.Task;
 }