geboshi_V1/LeatherProject/LeatherApp/Device/PhotoLib.cs

183 lines
5.2 KiB
C#
Raw Normal View History

2024-03-07 14:03:22 +08:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using LeatherApp.Utils;
using Models;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace LeatherApp.Device
{
public class PhotoLib : IDisposable
{
public Action<int> QueueCountEvent;//0/1/2, 数量
public Action<DateTime,WarningEnum, string> WarningEvent;
/// <summary>
/// 结果
/// </summary>
public Action<PhotoTask> finishEvent;
/// <summary>
/// 是否打开设备成功
/// </summary>
public bool IsInit { get; private set; } = false;
private Thread t_task1;
private List<PhotoTask> lstTask1 = new List<PhotoTask>();
//private Thread t_task2;
//private List<DefectTask> lstTask2 = new List<DefectTask>();
public PhotoLib()
{
}
public bool start()
{
try
{
IsInit = true;
lstTask1.Clear();
t_task1 = new System.Threading.Thread(run1);
t_task1.IsBackground = true;
t_task1.Start();
//t_task_operation = new System.Threading.Thread(run2);
//t_task_operation.IsBackground = true;
//t_task_operation.Start();
return true;
}
catch (Exception ex)
{
WarningEvent?.Invoke(DateTime.Now,WarningEnum.High, ex.Message);
return false;
}
}
public void stop()
{
if (!IsInit) return;
try
{
IsInit = false;
//timer.Elapsed -= Timer_Elapsed;
if (t_task1 != null)
{
bool b = t_task1.Join(5000);
if (!b) t_task1.Abort();
t_task1 = null;
}
//if (t_task_operation != null)
//{
// bool b = t_task_operation.Join(5000);
// if (!b) t_task_operation.Abort();
// t_task_operation = null;
//}
lstTask1.Clear();
//lstTask2.Clear();
}
catch { }
}
//推理+打标
private void run1()
{
while (IsInit)
{
if (lstTask1.Count < 1)
{
Thread.Sleep(0);
continue;
}
//
var task = pop();
//try
//{
if (task != null)
{
task.finishEvent(task);
}
//}
//catch (Exception ex)
//{
// WarningEvent?.Invoke(DateTime.Now,WarningEnum.Low, $"DefectLib task2 err({liStep}):" + ex.Message);
// task.isSucceed = false;
// task.resultInfo = ex.Message;
// callback(task);
//}
}
}
private void callback(PhotoTask task)
{
//返回成功/失败,异步调用
if (task.finishEvent != null || (task.finishEvent = finishEvent) != null)
//task.finishEvent.BeginInvoke(result, errInfo, res => task.finishEvent.EndInvoke(res), null);
System.Threading.ThreadPool.QueueUserWorkItem(waitCallback, task);
}
//异步回调
WaitCallback waitCallback = new WaitCallback(o =>
{
var task = (PhotoTask)o;
task.finishEvent(task);
});
public class PhotoTask
{
public Records record;
public object scanPhotos0;
public object scanPhotos1;
//
/// <summary>
/// 完成后回调
/// </summary>
public Action<PhotoTask> finishEvent;
//==结果返回
//public bool isSucceed;//转换是否成功
//public string resultInfo = "";//成功或失败信息
//public long[] stopwatch = new long[5];
}
public void add(PhotoTask task)
{
lock (lstTask1)
{
lstTask1.Add(task);
QueueCountEvent?.BeginInvoke(lstTask1.Count, null, null);
}
}
private PhotoTask pop()
{
lock (lstTask1)
{
if (lstTask1.Count < 1)
return null;
var task = lstTask1[0];
lstTask1.RemoveAt(0);
QueueCountEvent?.BeginInvoke(lstTask1.Count, null, null);
return task;
}
}
public void Dispose()
{
stop();
}
}
}