Exemple #1
0
        public virtual void Enqueue(object userState)
        {
            if (userState == null)
            {
                throw new ArgumentNullException("userState");
            }

            CurrentContext = userState as BackgroundActionContext;
            if (CurrentContext == null)
            {
                throw new NotSupportedException("Not support non BackgroundActionContext type userState");
            }

            lock (_theDelegateQueue)
            {
                _theDelegateQueue.Enqueue(CurrentContext.Task);
            }

            Start(userState);

            if (CurrentContext.BackgourndEndMode == EndMode.Abort)
            {
                Cancel();
            }
            else
            {
                NotifyBackgroundThread();
            }
        }
Exemple #2
0
        protected virtual void DoWork(object userState)
        {
            try
            {
                if (CancellationPending)
                {
                    Completed(this, new BackgroundTaskCompletedEventArgs(null, userState, true, null));
                }
                else
                {
                    BackgroundActionContext context = userState as BackgroundActionContext;
                    Func <object>           task    = context.Task;
                    if (task != null)
                    {
                        if (context.BackgourndEndMode != EndMode.Abort)
                        {
                            Starting(this, EventArgs.Empty);

                            IsBusy = true;
                            var result = task();
                            IsBusy = false;

                            Completed(this, new BackgroundTaskCompletedEventArgs(result, userState, false, null));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                IsBusy = false;
                Completed(this, new BackgroundTaskCompletedEventArgs(null, userState, CancellationPending, e));
                throw;
            }
        }
Exemple #3
0
        public static void BackgroundAction(object target, Action backgroundAction, Action befoe, Action callback, EndMode endMode, TaskImplMode taskMode)
        {
            var context = new BackgroundActionContext(endMode, () => { backgroundAction(); return null; });
            var container = target == null ? backgroundAction.Target.As<IExtendedPresenter>() : target.As<IExtendedPresenter>();
            var task = container.GetMetadata<IBackgroundThreadTask>();
            if (task == null)
            {
                task = TaskImplFactory.CreateTask(taskMode);
                if (taskMode != TaskImplMode.UIThread)
                {
                    container.WasShutdown += (s, e) => task.Dispose();
                    container.AddMetadata(task);
                }
            }

            //Before
            if (!befoe.IsNull())
                befoe();

            //Action
            task.Enqueue(context);

            //After
            if (!callback.IsNull())
                Application.Current.Dispatcher.BeginInvoke(callback, DispatcherPriority.ContextIdle);
        }
Exemple #4
0
        public static void BackgroundAction(object target, Action backgroundAction, Action befoe, Action callback, EndMode endMode, TaskImplMode taskMode)
        {
            var context   = new BackgroundActionContext(endMode, () => { backgroundAction(); return(null); });
            var container = target == null?backgroundAction.Target.As <IExtendedPresenter>() : target.As <IExtendedPresenter>();

            var task = container.GetMetadata <IBackgroundThreadTask>();

            if (task == null)
            {
                task = TaskImplFactory.CreateTask(taskMode);
                if (taskMode != TaskImplMode.UIThread)
                {
                    container.WasShutdown += (s, e) => task.Dispose();
                    container.AddMetadata(task);
                }
            }

            //Before
            if (!befoe.IsNull())
            {
                befoe();
            }

            //Action
            task.Enqueue(context);

            //After
            if (!callback.IsNull())
            {
                Application.Current.Dispatcher.BeginInvoke(callback, DispatcherPriority.ContextIdle);
            }
        }
Exemple #5
0
        protected virtual void DoWork(object userState)
        {
            try
            {
                if (CancellationPending)
                {
                    Completed(this, new BackgroundTaskCompletedEventArgs(null, userState, true, null));
                }
                else
                {
                    BackgroundActionContext context = userState as BackgroundActionContext;
                    Func <object>           task    = null;
                    lock (_theDelegateQueue)
                    {
                        if (_theDelegateQueue.Count > 0)
                        {
                            task = _theDelegateQueue.Dequeue();
                        }
                    }
                    if (task != null)
                    {
                        Starting(this, EventArgs.Empty);

                        IsBusy = true;
                        var result = task();
                        IsBusy = false;

                        Completed(this, new BackgroundTaskCompletedEventArgs(result, userState, false, null));

                        if (!CancellationPending)
                        {
                            IsWaiting = false;
                            if (context.BackgourndEndMode == EndMode.Continue)
                            {
                                if (_theDelegateQueue.Count == 0)
                                {
                                    IsWaiting = true;
                                    _autoReset.WaitOne();
                                }
                            }
                            else if (context.BackgourndEndMode == EndMode.End || context.BackgourndEndMode == EndMode.Abort)
                            {
                                return;
                            }
                            DoWork(CurrentContext);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                IsBusy = false;
                Completed(this, new BackgroundTaskCompletedEventArgs(null, userState, CancellationPending, e));
                throw;
            }
        }
Exemple #6
0
        protected void DoExecute(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
        {
            if (CurrentBackgroundActionInfo.BlockIfBusy && CurrentBackgroundTask.IsBusy)
            {
                return;
            }
            var context = new BackgroundActionContext(CurrentBackgroundActionInfo.BackgroundEndMode, () => _method.Invoke(handlingNode.MessageHandler.Unwrap(), parameters));

            CurrentBackgroundTask.Enqueue(context);
        }
        public virtual void Enqueue(object userState)
        {
            if (userState == null)
                throw new ArgumentNullException("userState");

            CurrentContext = userState as BackgroundActionContext;
            if (CurrentContext == null)
                throw new NotSupportedException("Not support non BackgroundActionContext type userState");

            lock (_theDelegateQueue)
            {
                _theDelegateQueue.Enqueue(CurrentContext.Task);
            }

            Start(userState);

            if (CurrentContext.BackgourndEndMode == EndMode.Abort)
                Cancel();
            else
                NotifyBackgroundThread();
        }
Exemple #8
0
 protected void DoExecute(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
 {
     if (CurrentBackgroundActionInfo.BlockIfBusy && CurrentBackgroundTask.IsBusy)
         return;
     var context = new BackgroundActionContext(CurrentBackgroundActionInfo.BackgroundEndMode, () => _method.Invoke(handlingNode.MessageHandler.Unwrap(), parameters));
     CurrentBackgroundTask.Enqueue(context);
 }