Commit ad4e221b 刘韬

优化监控相机重连机制

1 个父辈 958879e7
......@@ -345,6 +345,7 @@ namespace OnlineStore.DeviceLibrary
runStatus = StoreRunStatus.Wait;
mainTimer.Enabled = false;
CameraClose();
TimeSpan span = DateTime.Now - StartTime;
LogInfo(",停止运行,总运行时间:" + span.ToString());
}
......
......@@ -20,6 +20,7 @@ namespace OnlineStore.DeviceLibrary
public event EventHandler<Bitmap> camera_event;
public string CameraDeviceName = "monitor1";
bool loop = true;
public void LoadCameraConfig(string id)
{
string path = @".\StoreConfig\box_" + id + "_Camera.json";
......@@ -52,6 +53,7 @@ namespace OnlineStore.DeviceLibrary
{
LogUtil.error(Name + $"监控相机打开失败");
}
loop = true;
camerathread = new Thread(new ThreadStart(startCamera));
camerathread.Start();
GC.KeepAlive(camerathread);
......@@ -60,7 +62,7 @@ namespace OnlineStore.DeviceLibrary
int errortimes = 0;
void startCamera()
{
while (true)
while (loop)
{
try
{
......@@ -77,15 +79,13 @@ namespace OnlineStore.DeviceLibrary
}
else if (errortimes == 5)
{
camera.Open();
errortimes++;
camera.Close(CameraDeviceName);
Thread.Sleep(1000);
camera.Open(CameraDeviceName);
errortimes = 0;
LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
}
else if (errortimes == 6)
{
LogUtil.error(Name + $"相机连接失败, 相机线程退出,{errortimes}");
break;
}
Thread.Sleep(5000);
}
Thread.Sleep(1000 / 7);
}
catch
......@@ -93,9 +93,14 @@ namespace OnlineStore.DeviceLibrary
errortimes++;
}
}
camera.Close();
camera.Close(CameraDeviceName);
camera.Dispose();
}
public void CameraClose()
{
LogUtil.info(Name + "线程退出");
loop = false;
}
void CameraGrabOne(string filename)
{
try
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!