private async Task StopOperationAsync() { Stopwatch.Stop(); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { CaptureElement.Source = null; }); FrameReader.FrameArrived -= FrameReader_FrameArrived; try { await MediaCapture.StopPreviewAsync(); } catch (Exception e) when(e.HResult == unchecked ((int)0xc00dabe4) && MediaCapture.PreviewMediaCapture.CameraStreamState != CameraStreamState.Streaming) { // StopPreview is not idempotent, silence exception when camera is not streaming } await FrameReader.StopAsync(); FrameCollection.Dispose(); CollectedColorFrames.AsParallel().ForAll(frame => frame.Dispose()); CollectedNonIlluminatedInfraredFrames.AsParallel().ForAll(frame => frame.Dispose()); CollectedIlluminatedInfraredFrames.AsParallel().ForAll(frame => frame.Dispose()); CollectedColorFrames.Clear(); CollectedNonIlluminatedInfraredFrames.Clear(); CollectedIlluminatedInfraredFrames.Clear(); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, DispatcherTimer.Stop); }
private async void FrameCollection_CollectionProgressed(ExampleMediaFrameCollection sender, ExampleMediaFrameCollectionProgressedEventArgs args) { if (IsCollectionCompleted) { // Discard extraneous frames args.Frame.Dispose(); return; } if (args.Frame.SourceKind == MediaFrameSourceKind.Color) { CollectedColorFrames.Add(args.Frame); } else if (args.Frame.SourceKind == MediaFrameSourceKind.Infrared) { if (args.Frame.IsIlluminated == true) { CollectedIlluminatedInfraredFrames.Add(args.Frame); } else { CollectedNonIlluminatedInfraredFrames.Add(args.Frame); } } else { // Don't know how to handle, discard args.Frame.Dispose(); } if (IsCollectionCompleted) { FrameCollection.CollectionProgressed -= FrameCollection_CollectionProgressed; await WriteToDiskAsync(); await StopOperationAsync(); } }