using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FWindSoft.Revit { /// /// revit接收者创建类 /// public class RevitHandlerFactory { /* * 回调有两种整合方法; * 第一种是在Idling事件里进行拼装 * 第二种是开一个守护线程,等待事件结束标志,然后再执行; * * 如果是第一种的话,可以在handler控制,不需要单独去处理这种情况 * 所以采用第二种 * */ public static RevitHandler CreateWhileHandler(Func handler, Action callBack) { RevitHandler revitHandler = new RevitHandler(); revitHandler.Executing += (sender, args) => { TaskCompletionSource tcs = null; if (callBack != null) { tcs = new TaskCompletionSource(); tcs.Task.ContinueWith((t) => callBack(args)); } try { do { args.Result = handler(args); } while (args.Result == HandlerResult.Successed); } finally { tcs?.SetResult(null); } }; return revitHandler; } public static RevitHandler CreateWhileHandler(Func handler) { return CreateWhileHandler(handler, null); } public static RevitHandler CreateOnceHandler(Func handler, Action callBack) { RevitHandler revitHandler = new RevitHandler(); revitHandler.Executing += (sender, args) => { TaskCompletionSource tcs = null; if (callBack != null) { tcs = new TaskCompletionSource(); tcs.Task.ContinueWith((t) => callBack(args)); } try { //将基类属性填写到子类中 args.Result = handler(args); } finally { tcs?.SetResult(null); } }; return revitHandler; } public static RevitHandler CreateOnceHandler(Func handler) { return CreateOnceHandler(handler, null); } } }