//------------------------------------------------------------------------------------- /// <summary> /// /// </summary> /// <param name="task"></param> protected override void AsyncTaskDoneBody(AsyncTask task) { #region Persons if(task.TaskName == "Persons") { users = (Persons)task.Result; } #endregion Persons #region Security if(task.TaskName == "Security") { psec = (PulsarSecurity)task.Result; fdgvGroups.DataSource = new DictionaryBinder(psec.SecurityGroups); } #endregion Security #region AddGroup if(task.TaskName == "AddGroup") { SecurityGroup gr = (SecurityGroup)task.Tag; psec.SecurityGroups.Add(gr.SID, gr); } #endregion AddGroup #region UpdNameGroup if(task.TaskName == "UpdNameGroup") { SecurityGroup gr = (SecurityGroup)task.Tag; psec.SecurityGroups[gr.SID].Name = gr.Name; } #endregion UpdNameGroup #region UpdDescGroup if(task.TaskName == "UpdDescGroup") { SecurityGroup gr = (SecurityGroup)task.Tag; psec.SecurityGroups[gr.SID].Description = gr.Description; } #endregion UpdDescGroup #region DelGroup if(task.TaskName == "DelGroup") { SecurityGroup gr = (SecurityGroup)task.Tag; psec.SecurityGroups.Remove(gr.SID); } #endregion DelGroup #region SetParents if(task.TaskName == "SetParents") { ValuesPair<OID,List<OID>> i = (ValuesPair<OID,List<OID>>)task.Tag; psec.SetParentsSidLinks(i.Value1, i.Value2); fdgvGroups_SelectionChanged(null, null); } #endregion SetParents #region SetChilds if(task.TaskName == "SetChilds") { ValuesPair<OID,List<OID>> i = (ValuesPair<OID,List<OID>>)task.Tag; psec.SetChildSidLinks(i.Value1, i.Value2); fdgvGroups_SelectionChanged(null, null); } #endregion SetChilds }
//------------------------------------------------------------------------------------- /// protected override void AsyncTaskDoneBody(AsyncTask task) { #region Persons if(task.TaskName == "Persons") { _pers = (Persons)task.Result; ListBinder b = new ListBinder(_list = new PList<ISubject>(_pers)); b.CacheSort = false; b.Sort(Sort); fdgvList.DataSource = b; } #endregion Persons #region Save if(task.TaskName == "Save") { ISubject s = (ISubject)task.Tag; if(_pers.Contains(s) == false) _pers.Add((Person)s); else Pulsar.Serialization.PulsarSerializer.Deserialize(Pulsar.Serialization.PulsarSerializer.Backup(s), _pers[s.OID]); Modify = false; } #endregion Save #region Remove if(task.TaskName == "Remove") { _pers.Remove((Person)task.Tag); _list.Remove((ISubject)task.Tag); } #endregion Remove }
//------------------------------------------------------------------------------------- /// protected override void AsyncTaskDoneBody(AsyncTask task) { #region MainMenu if(task.TaskName == "MainMenu") { _menu = (PulsarMainMenu)task.Result; ftvTree.Tree = _menu; } #endregion MainMenu #region Save if(task.TaskName == "Save") { //ITreeItem sel = ftvTree.SelectedNodeItem; //ftvTree.Se } #endregion Save }
/// <summary> /// Выполняет асинхронную задачу. /// </summary> /// <param name="task">Объект выполняемой задачи.</param> public static void Run(AsyncTask task) { if (task == null) throw new ArgumentNullException("task"); lock (tasks) tasks.Add(task); lock (task) { task.thread = new Thread(() => { try { if (task.WorkMethod != null) task.Result = task.WorkMethod(); } catch (ThreadAbortException) { task.IsAborted = true; } catch (Exception exc) { task.Error = exc; } finally { lock (tasks) tasks.Remove(task); if (task.TaskDoneHandler != null) try { if (task.TaskDoneHandler.InvokeRequired) task.TaskDoneHandler.Invoke(task); else task.TaskDoneHandler.AsyncTaskDone(task); } catch { } if (task.are != null) task.are.Set(); } }); task.thread.IsBackground = true; task.thread.Name = "AsyncTask-" + task.TaskName; task.thread.Start(); } }
//------------------------------------------------------------------------------------- protected override void AsyncTaskDoneBody(AsyncTask task) { #region List if(task.TaskName == "List") { PulsarAnswer ans = (PulsarAnswer)task.Result; ToLog(ans.ToString()); fdgvList.DataSource = new ListBinder((IList)ans.Return); FillAnswer(ans, "Answer"); } #endregion List #region Query if(task.TaskName == "Query") { PulsarAnswer ans = (PulsarAnswer)task.Result; ToLog(ans.ToString()); FillAnswer(ans, "Answer"); } #endregion Query }
void IAsyncTaskDoneHandler.Invoke(AsyncTask task) { this.Invoke(new Action<AsyncTask>(((IAsyncTaskDoneHandler)this).AsyncTaskDone), task); }
//------------------------------------------------------------------------------------- void IAsyncTaskDoneHandler.AsyncTaskDone(AsyncTask task) { try { #region << Hide Progress Window >> Control pr = null; foreach (Control c in this.Controls) if (c is NetProgressControl) pr = c; else c.Enabled = true; this.Controls.Remove(pr); pr.Dispose(); pr = null; #endregion << Hide Progress Window >> #region << Error and Abort handler >> if (task.Error != null) { TaskManager.AbortTasks(this); ModalErrorBox.ShowServer(task.Error.Message, this); SimModalMessageBox.ShowError(this, "Инициализация программы завершилась с ошибкой!\r\n" + "Продолжение работы невозможно.", "Ошибка инициализации"); this.Close(); return; } if (task.IsAborted) return; #endregion << Error and Abort handler >> #region MainMenu if (task.TaskName == "MainMenu") { PulsarMainMenu menu = (PulsarMainMenu)task.Result; List<TabPage> pages = new List<TabPage>(); foreach (TreeItem<FormInfo> root in menu.GetRootItems().OrderBy(x=> x, PulsarMainMenu.PulsarMainMenuSorter.Default)) { string tab = root.Object.SD.ToString(); navigator1.AddTab(root.Object.Caption, tab); foreach (TreeItem<FormInfo> child in root.Children.OrderBy(x=> x, PulsarMainMenu.PulsarMainMenuSorter.Default)) NavigatorAddSubItems(menu, child.Object, navigator1[tab]); } navigator1.Tag = menu; navigator1.SelectTab((string)ServerParamsBase.GetParam("MainForm", "LastMenuTab", null)); } #endregion MainMenu } catch (Exception Err) { ModalErrorBox.Show(Err, this); SimModalMessageBox.ShowError(this, "Инициализация программы завершилась с ошибкой!\r\n" + "Продолжение работы невозможно.", "Ошибка инициализации"); this.Close(); } }
//------------------------------------------------------------------------------------- void IAsyncTaskDoneHandler.Invoke(AsyncTask task) { PanelBack.Invoke(new Action<AsyncTask>(AsyncTaskDone), task); }
//------------------------------------------------------------------------------------- /// <summary> /// Метод тела метода, вызываемый при завершении асинхронных задач формы. /// </summary> /// <param name="task">Асинхронная задача.</param> protected virtual void AsyncTaskDoneBody(AsyncTask task) { }
//------------------------------------------------------------------------------------- /// <summary> /// Метод, вызываемый при завершении асинхронных задач формы. /// Реализация по умолчанию содержит проверки на ошибки и прерывания, вызов AsyncTaskDoneBody и проверку /// на закрытие окна прогресса. /// </summary> /// <param name="task">Асинхронная задача.</param> public virtual void AsyncTaskDone(AsyncTask task) { try { #region << Error and Abort handler >> if(task.Error != null) { TaskManager.AbortTasks(this); HideProgressWindow(); if (task.Error is System.Reflection.TargetInvocationException) ModalErrorBox.Show(task.Error.InnerException, PanelBack); else if(task.Error is PulsarServerException) ModalErrorBox.ShowServer(task.Error.Message, PanelBack); else ModalErrorBox.Show(task.Error, PanelBack); return; } if(task.IsAborted) return; #endregion << Error and Abort handler >> AsyncTaskDoneBody(task); if (AsyncTaskCompleted != null) OnAsyncTaskCompleted(new EventArgs<AsyncTask>(task)); } catch(Exception Err) { Sim.Controls.ErrorBox.Show(Err); } finally { if(task.IsQueued == false && TaskManager.ContainsTask(this) == false) HideProgressWindow(); } }
//------------------------------------------------------------------------------------- /// <summary> /// /// </summary> /// <param name="task"></param> protected override void AsyncTaskDoneBody(AsyncTask task) { #region Persons if(task.TaskName == "Persons") { users = (Persons)task.Result; comboBoxUsers.Items.Add(new ComboBoxItem<Person>(null, " (Bсе)")); foreach(Person u in users) comboBoxUsers.Items.Add(new ComboBoxItem<Person>(u)); } #endregion Persons #region Security if(task.TaskName == "Security") { pSec = (PulsarSecurity)task.Result; } #endregion Security #region MainMenu if(task.TaskName == "MainMenu") { mMenu = (PulsarMainMenu)task.Result; } #endregion MainMenu #region SetACEsForSD if(task.TaskName == "SetACEsForSD") { ValuesPair<OID, PList<ACE>> i = (ValuesPair<OID, PList<ACE>>)task.Tag; pSec.SetACEsForSD(i.Value1, i.Value2); btnCancel_Click(btnCancel, EventArgs.Empty); RecheckUserAccess(); } #endregion SetACEsForSD }
//------------------------------------------------------------------------------------- #region << Methods >> /// <summary> /// Создает асинхронную задачу и выполняет ее. /// </summary> /// <param name="taskName">Имя задачи.</param> /// <param name="dst">Объект, обрабатывающий уведомления о завершении задачи.</param> /// <param name="workMethod">Рабочий метод задачи.</param> /// <param name="tag">Дополнительная информацияю</param> /// <returns>Объект асинхронной задачи.</returns> public static AsyncTask Run(string taskName, IAsyncTaskDoneHandler dst, Func<object> workMethod, object tag = null) { AsyncTask task = new AsyncTask(taskName, dst) { WorkMethod = workMethod, Tag = tag }; Run(task); return task; }