private void StartFaceDetection(MediaCapture capture) { if (capture == null) { throw new ArgumentNullException(); } if (_faceDetectionCancellationTokenSource != null) { return; } _faceDetectionCancellationTokenSource = new CancellationTokenSource(); var token = _faceDetectionCancellationTokenSource.Token; _faceDetectionTask = Task.Factory.StartNew(async() => { try { FaceDetector detector = new FaceDetector(); detector.Initialize(new FaceDetectorInitializationData() { FaceData = Package.Current.InstalledLocation.Path + "\\" + FacePredictorFileName }); while (!token.IsCancellationRequested) { await FaceDetectAsync(detector, capture, token); } } catch (OperationCanceledException) { } catch (Exception e) { Debug.WriteLine("Face detection failed."); Debug.WriteLine(e.Message); } }, token); }