IEnumerator ThreadCoroutine(System.Object o) { ThreadComm comm = o as ThreadComm; while (true) { while (!comm.shouldDetectInMultiThread) { yield return(null); } lock (thisLock) { MatOfRect faces = new MatOfRect(); if (cascade != null) { cascade.detectMultiScale(grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size(grayMat4Thread.height() * 0.2, grayMat4Thread.height() * 0.2), new Size()); } resultDetect = faces; } comm.shouldDetectInMultiThread = false; didUpdateTheDetectionResult = true; } }
private void _ThreadWorker(System.Object o) { ThreadComm comm = o as ThreadComm; while (!shouldStopThread) { if (!comm.shouldDetectInMultiThread) { continue; } lock (thisLock) { MatOfRect faces = new MatOfRect(); if (cascade != null) { cascade.detectMultiScale(grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size(grayMat4Thread.height() * 0.2, grayMat4Thread.height() * 0.2), new Size()); } resultDetect = faces; } comm.shouldDetectInMultiThread = false; didUpdateTheDetectionResult = true; } isThreadRunning = false; }
private void ThreadWorker() { if (isThreadRunning) { return; } Debug.Log("Thread Start"); isThreadRunning = true; threadComm.shouldDetectInMultiThread = false; didUpdateTheDetectionResult = false; tokenSource_ = new CancellationTokenSource(); task_ = Task.Factory.StartNew( (o) => { ThreadComm comm = o as ThreadComm; while (true) { tokenSource_.Token.ThrowIfCancellationRequested(); if (!comm.shouldDetectInMultiThread) { continue; } lock (thisLock) { MatOfRect faces = new MatOfRect(); if (cascade != null) { cascade.detectMultiScale(grayMat4Thread, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, // TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size(grayMat4Thread.height() * 0.2, grayMat4Thread.height() * 0.2), new Size()); } resultDetect = faces; } comm.shouldDetectInMultiThread = false; didUpdateTheDetectionResult = true; } } , threadComm , tokenSource_.Token ).ContinueWith(t => { tokenSource_.Dispose(); tokenSource_ = null; isThreadRunning = false; }); }