From 4648ac01fec2a24c58d8741fbf4b6e0b2d1b79e8 Mon Sep 17 00:00:00 2001 From: DucDangAnh Date: Fri, 19 Jun 2020 15:35:34 +0700 Subject: [PATCH] Camera - Add Method Start, Stop, Destructor. --- AIParkingApplication/Camera.cs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/AIParkingApplication/Camera.cs b/AIParkingApplication/Camera.cs index d469e6c..c162a69 100644 --- a/AIParkingApplication/Camera.cs +++ b/AIParkingApplication/Camera.cs @@ -7,9 +7,8 @@ namespace AIParkingApplication public class Camera { private string streamUrl; - private bool isCapturing; - private object lockSyncObject; private volatile bool isFrameRequested; + private Thread readStreamThread; public event CameraEvent OnVideoFrameReceived; public event CameraEvent OnOneVideoFrameRequested; @@ -17,16 +16,24 @@ namespace AIParkingApplication public Camera(string streamUrl) { this.streamUrl = streamUrl; - isCapturing = true; isFrameRequested = false; - lockSyncObject = new object(); + readStreamThread = new Thread(new ThreadStart(ReadVideoStream)); + readStreamThread.IsBackground = true; } - public void Startcapture() + ~Camera() { - Thread readVideoStreamThread = new Thread(new ThreadStart(ReadVideoStream)); - readVideoStreamThread.IsBackground = true; - readVideoStreamThread.Start(); + Stop(); + } + + public void Start() + { + if (!readStreamThread.IsAlive) + { + readStreamThread = new Thread(new ThreadStart(ReadVideoStream)); + readStreamThread.IsBackground = true; + readStreamThread.Start(); + } } public void RequestCaptureOneFrame() @@ -47,10 +54,6 @@ namespace AIParkingApplication { Thread.Sleep(50); //Stream Thread Sleep //Thread.Sleep(40); //Video Thread Sleep - if (!isCapturing) - { - continue; - } if (videoCapture.Read(videoFrame) && videoFrame.Width > 0 && videoFrame.Height > 0) { @@ -64,11 +67,11 @@ namespace AIParkingApplication } } - public void PauseCapture() + public void Stop() { - lock (lockSyncObject) + if (readStreamThread.IsAlive) { - isCapturing = false; + readStreamThread.Abort(); } } }