| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.ToolBlock;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace TeamAAS_VP.Helpers
- {
- /// <summary>
- /// VisionPro 工具辅助类。
- /// 提供在独立 STA 线程中异步执行 CogToolBlock 的能力,
- /// 避免 COM 封送到主 UI 线程导致界面假死。
- /// </summary>
- public static class VisionProHelper
- {
- /// <summary>
- /// 在专用 STA 后台线程中异步运行 ToolBlock。
- /// 每次调用创建一个新的 STA 线程,ToolBlock 执行完毕后线程自动退出。
- /// </summary>
- /// <param name="toolBlock">要执行的 CogToolBlock 实例</param>
- /// <returns>异步任务,完成时表示 ToolBlock 执行完毕</returns>
- public static Task RunToolBlockAsync(CogToolBlock toolBlock)
- {
- if (toolBlock == null)
- throw new ArgumentNullException(nameof(toolBlock));
- var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
- var thread = new Thread(() =>
- {
- try
- {
- toolBlock.Run();
- tcs.TrySetResult(true);
- }
- catch (Exception ex)
- {
- tcs.TrySetException(ex);
- }
- })
- {
- IsBackground = true,
- Name = $"VP-Run-{Guid.NewGuid():N}"
- };
- thread.SetApartmentState(ApartmentState.STA);
- thread.Start();
- return tcs.Task;
- }
- /// <summary>
- /// 在专用 STA 后台线程中异步加载 VisionPro 序列化对象(.vpp 文件等)。
- /// </summary>
- /// <typeparam name="T">反序列化的目标类型</typeparam>
- /// <param name="filePath">.vpp 文件完整路径</param>
- /// <returns>包含反序列化对象的异步任务</returns>
- public static Task<T> LoadVisionProObjectAsync<T>(string filePath) where T : class
- {
- if (string.IsNullOrEmpty(filePath))
- throw new ArgumentNullException(nameof(filePath));
- var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
- var thread = new Thread(() =>
- {
- try
- {
- var obj = CogSerializer.LoadObjectFromFile(filePath) as T;
- tcs.TrySetResult(obj);
- }
- catch (Exception ex)
- {
- tcs.TrySetException(ex);
- }
- })
- {
- IsBackground = true,
- Name = $"VP-Load-{Guid.NewGuid():N}"
- };
- thread.SetApartmentState(ApartmentState.STA);
- thread.Start();
- return tcs.Task;
- }
- /// <summary>
- /// 在专用 STA 后台线程中异步保存 VisionPro 对象到文件。
- /// </summary>
- /// <param name="obj">要保存的对象</param>
- /// <param name="filePath">目标文件路径</param>
- /// <returns>异步任务</returns>
- public static Task SaveVisionProObjectAsync(object obj, string filePath)
- {
- if (obj == null) throw new ArgumentNullException(nameof(obj));
- if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath));
- var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
- var thread = new Thread(() =>
- {
- try
- {
- CogSerializer.SaveObjectToFile(obj, filePath, typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter),
- CogSerializationOptionsConstants.Minimum);
- tcs.TrySetResult(true);
- }
- catch (Exception ex)
- {
- tcs.TrySetException(ex);
- }
- })
- {
- IsBackground = true,
- Name = $"VP-Save-{Guid.NewGuid():N}"
- };
- thread.SetApartmentState(ApartmentState.STA);
- thread.Start();
- return tcs.Task;
- }
- }
- }
|