Ejemplo n.º 1
0
        public TreeTask BeginRefreshTask(
            TreeNode treeNode,
            bool hierarchically,
            NodeUpdatingSource mode,
            Action continueWith = null
            )
        {
            if (treeNode == null)
            {
                return(null);
            }

            ConcreteTemplateNodeDefinition templateDef =
                treeNode.Tag as ConcreteTemplateNodeDefinition;

            if (templateDef == null)
            {
                log.Debug("MSSQLServerAuditor.TreeTaskManager:BeginRefreshTask templateDef is not defined.");
                return(null);
            }

            long nodeHandle = templateDef.ComputeHandle();

            if (this.RunningTasks.ContainsKey(nodeHandle))
            {
                return(null);
            }

            ConnectionData connectionData = GetConnectionData(treeNode);

            TreeTaskInfo taskInfo = new TreeTaskInfo
            {
                Connection     = connectionData,
                Mode           = mode,
                Hierarchically = hierarchically,
                Note           = string.Format("{0}", DateTime.Now.ToString("mm:ss")),
                Handle         = nodeHandle
            };

            TreeTask treeTask = TreeTask.Create(taskInfo);

            if (this._treeControl != null)
            {
                treeTask.Progress.ProgressChanged +=
                    (sender, args) => this._treeControl.SetProgressValue((int)args.NewValue);
            }

            treeTask.Completed += (sender, args) =>
            {
                Task.Factory.StartNew(() => RemoveClosedTask(treeTask));

                if (continueWith != null)
                {
                    continueWith();
                }
            };

            lock (this._runningTasksLock)
            {
                this.RunningTasks.TryAdd(taskInfo.Handle, treeTask);
            }

            // don't change order of Add and Subscribe!
            treeTask.JobChanged += OnTaskJobChanged;

            if (this._treeControl != null)
            {
                this._treeControl.SetInProgress(taskInfo, true, true);
            }

            ProgressItem progress = new ProgressItem();

            RefreshNode(treeTask, null, treeNode, progress);

            return(treeTask);
        }