public void Execute(object commandContext)
        {
            if (CanExecute(commandContext))
            {
                if (commandContext is T t)
                {
                    try
                    {
                        t.Prepare();
                        t.StartTime         = DateTime.Now;
                        t.CommandBaseCommon = this;
                        if (IsCommandExecLogEnabled)
                        {
                            t.Log($"start command '{GetType().Name}' at {t.StartTime}");
                        }

                        if (!t.RunAsParallelTask)
                        {
                            ExecuteImpl(t);
                            t.EndTime = DateTime.Now;
                            if (IsCommandExecLogEnabled)
                            {
                                t.Log($"command '{GetType().Name}' ended at {t.EndTime}");
                            }
                        }
                        else
                        {
                            var task =
                                new MonitoredTask <T>(
                                    GetType().Name,
                                    ExecuteImpl,
                                    t
                                    );
                            t.MonitoredTask = task;
                            if (t.WaitTaskCompleted)
                            {
                                task.Task.Wait();
                                t.EndTime = DateTime.Now;
                                if (IsCommandExecLogEnabled)
                                {
                                    t.Log($"command '{GetType().Name}' ended at {t.EndTime}");
                                }
                            }
                        }
                    } catch (Exception ex)
                    {
                        t.Exception = ex;
                        throw;
                    }
                }
                else
                {
                    throw new Exception($"wrong command context type: {commandContext}");
                }
            }
        }
Exemplo n.º 2
0
        public MonitoredTask MonitorTask(Task t, CancellationTokenSource cancelToken)
        {
            var mt = new MonitoredTask()
            {
                task       = t,
                cancelTask = () => { cancelToken.Cancel(); }
            };

            monitoredTasks.Enqueue(mt);
            return(mt);
        }
Exemplo n.º 3
0
        public MonitoredTask <T> RunInBackground <T>(Func <CancellationToken, Task <T> > asyncFunction, TaskScheduler scheduler, TaskCreationOptions o = TaskCreationOptions.None)
        {
            var      cancelToken = new CancellationTokenSource();
            Task <T> task        = TaskFactoryStartNew(() => asyncFunction(cancelToken.Token),
                                                       cancelToken.Token, o, scheduler).Unwrap();
            var mt = new MonitoredTask <T>()
            {
                task = task, cancelTask = () => { cancelToken.Cancel(); }
            };

            return(AddToMonitoredTasks(mt, cancelToken));
        }