RevitHandlerFactory.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace FWindSoft.Revit
  7. {
  8. /// <summary>
  9. /// revit接收者创建类
  10. /// </summary>
  11. public class RevitHandlerFactory
  12. {
  13. /*
  14. * 回调有两种整合方法;
  15. * 第一种是在Idling事件里进行拼装
  16. * 第二种是开一个守护线程,等待事件结束标志,然后再执行;
  17. *
  18. * 如果是第一种的话,可以在handler控制,不需要单独去处理这种情况
  19. * 所以采用第二种
  20. *
  21. */
  22. public static RevitHandler CreateWhileHandler(Func<RevitEventArgs, HandlerResult> handler, Action<RevitEventArgs> callBack)
  23. {
  24. RevitHandler revitHandler = new RevitHandler();
  25. revitHandler.Executing += (sender, args) =>
  26. {
  27. TaskCompletionSource<object> tcs = null;
  28. if (callBack != null)
  29. {
  30. tcs = new TaskCompletionSource<object>();
  31. tcs.Task.ContinueWith((t) => callBack(args));
  32. }
  33. try
  34. {
  35. do
  36. {
  37. args.Result = handler(args);
  38. } while (args.Result == HandlerResult.Successed);
  39. }
  40. finally
  41. {
  42. tcs?.SetResult(null);
  43. }
  44. };
  45. return revitHandler;
  46. }
  47. public static RevitHandler CreateWhileHandler(Func<RevitEventArgs, HandlerResult> handler)
  48. {
  49. return CreateWhileHandler(handler, null);
  50. }
  51. public static RevitHandler CreateOnceHandler(Func<RevitEventArgs, HandlerResult> handler, Action<RevitEventArgs> callBack)
  52. {
  53. RevitHandler revitHandler = new RevitHandler();
  54. revitHandler.Executing += (sender, args) =>
  55. {
  56. TaskCompletionSource<object> tcs = null;
  57. if (callBack != null)
  58. {
  59. tcs = new TaskCompletionSource<object>();
  60. tcs.Task.ContinueWith((t) => callBack(args));
  61. }
  62. try
  63. {
  64. //将基类属性填写到子类中
  65. args.Result = handler(args);
  66. }
  67. finally
  68. {
  69. tcs?.SetResult(null);
  70. }
  71. };
  72. return revitHandler;
  73. }
  74. public static RevitHandler CreateOnceHandler(Func<RevitEventArgs, HandlerResult> handler)
  75. {
  76. return CreateOnceHandler(handler, null);
  77. }
  78. }
  79. }