using Autodesk.Revit.UI; using Autodesk.Revit.UI.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FWindSoft.Revit { /// /// 业务轮询命令 /// public class IdlingEventCommand: IRevitRegister { #region 初始化 public IdlingEventCommand() { Commands = new IdlingCommands(); } /// /// 命令队列 /// private readonly IdlingCommands Commands; #endregion #region 消费队列 /// /// 对接事件绑定 /// /// /// protected void ApplicaionIdling(object sender, IdlingEventArgs e) { IdlingCommand currentCommand = Commands.Dequeue(); if (currentCommand != null) { currentCommand.Raise(sender as UIApplication); e.SetRaiseWithoutDelay(); } } #endregion #region 生产队列 public IdlingCommand OnceExecute(Func handler, Action callBack) { IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateOnceHandler(handler, callBack)); Commands.Enqueue(command); return command; } public IdlingCommand WhileExecute(Func handler, Action callBack) { IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateWhileHandler(handler, callBack)); Commands.Enqueue(command); return command; } public IdlingCommand Execute(IdlingCommand command) { if (command == null) return null; Commands.Enqueue(command); return command; } #endregion #region 事件注册相关 public void Register(ExternalApplication application) { application.Idling += ApplicaionIdling; } public void Unregister(ExternalApplication application) { application.Idling -= ApplicaionIdling; } #endregion } }