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