VisionProHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace TeamAAS_VP.Helpers
  7. {
  8. /// <summary>
  9. /// VisionPro 工具辅助类。
  10. /// 提供在独立 STA 线程中异步执行 CogToolBlock 的能力,
  11. /// 避免 COM 封送到主 UI 线程导致界面假死。
  12. /// </summary>
  13. public static class VisionProHelper
  14. {
  15. /// <summary>
  16. /// 在专用 STA 后台线程中异步运行 ToolBlock。
  17. /// 每次调用创建一个新的 STA 线程,ToolBlock 执行完毕后线程自动退出。
  18. /// </summary>
  19. /// <param name="toolBlock">要执行的 CogToolBlock 实例</param>
  20. /// <returns>异步任务,完成时表示 ToolBlock 执行完毕</returns>
  21. public static Task RunToolBlockAsync(CogToolBlock toolBlock)
  22. {
  23. if (toolBlock == null)
  24. throw new ArgumentNullException(nameof(toolBlock));
  25. var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
  26. var thread = new Thread(() =>
  27. {
  28. try
  29. {
  30. toolBlock.Run();
  31. tcs.TrySetResult(true);
  32. }
  33. catch (Exception ex)
  34. {
  35. tcs.TrySetException(ex);
  36. }
  37. })
  38. {
  39. IsBackground = true,
  40. Name = $"VP-Run-{Guid.NewGuid():N}"
  41. };
  42. thread.SetApartmentState(ApartmentState.STA);
  43. thread.Start();
  44. return tcs.Task;
  45. }
  46. /// <summary>
  47. /// 在专用 STA 后台线程中异步加载 VisionPro 序列化对象(.vpp 文件等)。
  48. /// </summary>
  49. /// <typeparam name="T">反序列化的目标类型</typeparam>
  50. /// <param name="filePath">.vpp 文件完整路径</param>
  51. /// <returns>包含反序列化对象的异步任务</returns>
  52. public static Task<T> LoadVisionProObjectAsync<T>(string filePath) where T : class
  53. {
  54. if (string.IsNullOrEmpty(filePath))
  55. throw new ArgumentNullException(nameof(filePath));
  56. var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
  57. var thread = new Thread(() =>
  58. {
  59. try
  60. {
  61. var obj = CogSerializer.LoadObjectFromFile(filePath) as T;
  62. tcs.TrySetResult(obj);
  63. }
  64. catch (Exception ex)
  65. {
  66. tcs.TrySetException(ex);
  67. }
  68. })
  69. {
  70. IsBackground = true,
  71. Name = $"VP-Load-{Guid.NewGuid():N}"
  72. };
  73. thread.SetApartmentState(ApartmentState.STA);
  74. thread.Start();
  75. return tcs.Task;
  76. }
  77. /// <summary>
  78. /// 在专用 STA 后台线程中异步保存 VisionPro 对象到文件。
  79. /// </summary>
  80. /// <param name="obj">要保存的对象</param>
  81. /// <param name="filePath">目标文件路径</param>
  82. /// <returns>异步任务</returns>
  83. public static Task SaveVisionProObjectAsync(object obj, string filePath)
  84. {
  85. if (obj == null) throw new ArgumentNullException(nameof(obj));
  86. if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath));
  87. var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
  88. var thread = new Thread(() =>
  89. {
  90. try
  91. {
  92. CogSerializer.SaveObjectToFile(obj, filePath);
  93. tcs.TrySetResult(true);
  94. }
  95. catch (Exception ex)
  96. {
  97. tcs.TrySetException(ex);
  98. }
  99. })
  100. {
  101. IsBackground = true,
  102. Name = $"VP-Save-{Guid.NewGuid():N}"
  103. };
  104. thread.SetApartmentState(ApartmentState.STA);
  105. thread.Start();
  106. return tcs.Task;
  107. }
  108. }
  109. }