using Autodesk.Revit.UI; using System; using System.Threading.Tasks; namespace RevitTools.Utils.Revit { /// /// 外部事件工作项 /// internal class ExEventWorkItem { /// /// 异步阻塞信号 /// public TaskCompletionSource CompletionSource { get; protected set; } /// /// 在外部事件中执行的, 带有一个返回值的函数 /// private Action Action { get; set; } /// /// 在外部事件中执行的, 带有一个返回值的函数 /// private Func Func { get; set; } /// /// 执行外部事件函数 /// /// public void InvokeAction(UIApplication application) { try { if (Action != null) { Action.Invoke(application); this.CompletionSource.TrySetResult(null); } else { var result = Func.Invoke(application); this.CompletionSource.TrySetResult(result); } } catch (Exception ex) { this.CompletionSource.TrySetException(ex); } } public ExEventWorkItem(Action action) { this.CompletionSource = new TaskCompletionSource(); this.Action = action; } public ExEventWorkItem(Func action) { this.CompletionSource = new TaskCompletionSource(); this.Func = action; } } }