革博士程序V1仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 regels
5.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Runtime.InteropServices;
  11. using System.Threading;
  12. using LeatherApp.Utils;
  13. using Models;
  14. using OpenCvSharp;
  15. using OpenCvSharp.Extensions;
  16. namespace LeatherApp.Device
  17. {
  18. public class PhotoLib : IDisposable
  19. {
  20. public Action<int> QueueCountEvent;//0/1/2, 数量
  21. public Action<DateTime,WarningEnum, string> WarningEvent;
  22. /// <summary>
  23. /// 结果
  24. /// </summary>
  25. public Action<PhotoTask> finishEvent;
  26. /// <summary>
  27. /// 是否打开设备成功
  28. /// </summary>
  29. public bool IsInit { get; private set; } = false;
  30. private Thread t_task1;
  31. private List<PhotoTask> lstTask1 = new List<PhotoTask>();
  32. //private Thread t_task2;
  33. //private List<DefectTask> lstTask2 = new List<DefectTask>();
  34. public PhotoLib()
  35. {
  36. }
  37. public bool start()
  38. {
  39. try
  40. {
  41. IsInit = true;
  42. lstTask1.Clear();
  43. t_task1 = new System.Threading.Thread(run1);
  44. t_task1.IsBackground = true;
  45. t_task1.Start();
  46. //t_task_operation = new System.Threading.Thread(run2);
  47. //t_task_operation.IsBackground = true;
  48. //t_task_operation.Start();
  49. return true;
  50. }
  51. catch (Exception ex)
  52. {
  53. WarningEvent?.Invoke(DateTime.Now,WarningEnum.High, ex.Message);
  54. return false;
  55. }
  56. }
  57. public void stop()
  58. {
  59. if (!IsInit) return;
  60. try
  61. {
  62. IsInit = false;
  63. //timer.Elapsed -= Timer_Elapsed;
  64. if (t_task1 != null)
  65. {
  66. bool b = t_task1.Join(5000);
  67. if (!b) t_task1.Abort();
  68. t_task1 = null;
  69. }
  70. //if (t_task_operation != null)
  71. //{
  72. // bool b = t_task_operation.Join(5000);
  73. // if (!b) t_task_operation.Abort();
  74. // t_task_operation = null;
  75. //}
  76. lstTask1.Clear();
  77. //lstTask2.Clear();
  78. }
  79. catch { }
  80. }
  81. //推理+打标
  82. private void run1()
  83. {
  84. while (IsInit)
  85. {
  86. if (lstTask1.Count < 1)
  87. {
  88. Thread.Sleep(0);
  89. continue;
  90. }
  91. //
  92. var task = pop();
  93. //try
  94. //{
  95. if (task != null)
  96. {
  97. task.finishEvent(task);
  98. }
  99. //}
  100. //catch (Exception ex)
  101. //{
  102. // WarningEvent?.Invoke(DateTime.Now,WarningEnum.Low, $"DefectLib task2 err({liStep}):" + ex.Message);
  103. // task.isSucceed = false;
  104. // task.resultInfo = ex.Message;
  105. // callback(task);
  106. //}
  107. }
  108. }
  109. private void callback(PhotoTask task)
  110. {
  111. //返回成功/失败,异步调用
  112. if (task.finishEvent != null || (task.finishEvent = finishEvent) != null)
  113. //task.finishEvent.BeginInvoke(result, errInfo, res => task.finishEvent.EndInvoke(res), null);
  114. System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, task);
  115. }
  116. //异步回调
  117. WaitCallback waitCallback = new WaitCallback(o =>
  118. {
  119. var task = (PhotoTask)o;
  120. task.finishEvent(task);
  121. });
  122. public class PhotoTask
  123. {
  124. public Records record;
  125. public object scanPhotos0;
  126. public object scanPhotos1;
  127. //
  128. /// <summary>
  129. /// 完成后回调
  130. /// </summary>
  131. public Action<PhotoTask> finishEvent;
  132. //==结果返回
  133. //public bool isSucceed;//转换是否成功
  134. //public string resultInfo = "";//成功或失败信息
  135. //public long[] stopwatch = new long[5];
  136. }
  137. public void add(PhotoTask task)
  138. {
  139. lock (lstTask1)
  140. {
  141. lstTask1.Add(task);
  142. QueueCountEvent?.BeginInvoke(lstTask1.Count, null, null);
  143. }
  144. }
  145. private PhotoTask pop()
  146. {
  147. lock (lstTask1)
  148. {
  149. if (lstTask1.Count < 1)
  150. return null;
  151. var task = lstTask1[0];
  152. lstTask1.RemoveAt(0);
  153. QueueCountEvent?.BeginInvoke(lstTask1.Count, null, null);
  154. return task;
  155. }
  156. }
  157. public void Dispose()
  158. {
  159. stop();
  160. }
  161. }
  162. }