private void MainPage_Loaded(object sender, RoutedEventArgs e) { try { // this.Background = new SolidColorBrush(Color.FromArgb(255,128, 128, 128)); if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.IoT") { // "Windows.Desktop" // "Windows.Mobile" (phone) // "Windows.IoT" _fUseNetworkTime = true; } Action resetCameras = () => { lock (_timerLock) { _cameraDevices = null;// force reload } }; var deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.VideoCapture); deviceWatcher.Added += new TypedEventHandler <DeviceWatcher, DeviceInformation>( (wat, info) => { resetCameras(); }); deviceWatcher.Removed += new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>( (wat, info) => { resetCameras(); }); deviceWatcher.Updated += new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>( (wat, info) => { resetCameras(); }); deviceWatcher.Stopped += new TypedEventHandler <DeviceWatcher, object>( (wat, obj) => { deviceWatcher.Start(); }); deviceWatcher.Start(); var relPanel = new RelativePanel(); var spCtrls = new StackPanel() { Orientation = Orientation.Horizontal }; _img.HorizontalAlignment = HorizontalAlignment.Center; _img.Stretch = Stretch.UniformToFill; _btnSwitchCamera = new Button() { IsEnabled = _cameraDevices?.Count > 1, Width = 260 }; SetBtnSwitchLabel(); ToolTipService.SetToolTip(_btnSwitchCamera, new ToolTip() { Content = "Click to switch camera if available" }); spCtrls.Children.Add(_btnSwitchCamera); _btnSwitchCamera.Click += (oc, ec) => { IncrementCameraInUse(); SetBtnSwitchLabel(); }; _chkCycleCameras = new CheckBox() { Content = "Cycle Cameras", IsChecked = false }; ToolTipService.SetToolTip(_chkCycleCameras, new ToolTip() { Content = "Automatically switch through all attached cameras" }); spCtrls.Children.Add(_chkCycleCameras); relPanel.Children.Add(spCtrls); var tbInterval = new TextBox() { Text = "7" }; spCtrls.Children.Add(tbInterval); var btnQuit = new Button() { Content = "Quit" }; spCtrls.Children.Add(btnQuit); btnQuit.Click += (oq, eq) => { lock (_timerLock) { // make sure we're done with cam before exit Application.Current.Exit(); } }; spCtrls.Children.Add(_tbStatus); relPanel.Children.Add(_img); RelativePanel.SetBelow(_img, spCtrls); var tmr = new DispatcherTimer(); tmr.Interval = TimeSpan.FromSeconds(4); tbInterval.LostFocus += (otb, etb) => { double n; if (double.TryParse(tbInterval.Text, out n)) { tmr.Interval = TimeSpan.FromSeconds(n); } }; bool fIsInTickRoutine = false; _tsSinceLastTimeCheck = TimeSpan.FromDays(1); // force time check tmr.Tick += async(ot, et) => { if (!fIsInTickRoutine) { fIsInTickRoutine = true; if (Monitor.TryEnter(_timerLock)) { try { if (_fUseNetworkTime) { _tsSinceLastTimeCheck += tmr.Interval; if (_tsSinceLastTimeCheck.TotalMinutes >= 1) { // resync the clock try { _dtLastTimeCheck = await NtpClient.GetDateTimeAsync(); _tsSinceLastTimeCheck = TimeSpan.Zero; } catch (Exception ex) { _tbStatus.Text = ex.ToString(); // task cancelled exception } } } await LookForCameraAndTakeAPicture(); } finally { Monitor.Exit(_timerLock); } } fIsInTickRoutine = false; } }; tmr.Start(); this.Content = relPanel; } catch (Exception ex) { this.Content = new TextBlock() { Text = ex.ToString() }; } }
public async static Task <DateTime> GetDateTimeAsync() { var ntpClient = new NtpClient(); return(await ntpClient.GetNetworkTimeAsync()); }