private async void startLiveViewBtn_Click(object sender, EventArgs e) { if (!isPreviewRunning_) { if (previewWindow_ == null) { previewWindow_ = new PreviewWindow(); previewWindow_.FormClosing += new FormClosingEventHandler( // Perform click will cause calling startLiveViewBtn() method, which stop preview and clean resources (object s, FormClosingEventArgs args) => startLiveViewBtn.PerformClick() ); } liveViewService_.NvrPreviewSettings = new NvrPreviewSettings { PreviewHandleMode = (PreviewHandleType)comboBox5.SelectedIndex, LinkMode = (PreviewLinkModeType)comboBox2.SelectedIndex, StreamType = (uint)numericUpDown1.Value, DisplayBufNum = (uint)numericUpDown2.Value, IsBlocked = checkBox2.Checked, IsPassbackRecord = checkBox1.Checked, PlayerBufSize = parseBufferSize(textBox5.Text), PreviewMode = (PreviewModeType)comboBox4.SelectedIndex, PreviewQuality = (PreviewQualityType)comboBox3.SelectedIndex }; try { liveViewService_.StartLiveView(previewWindow_.GetLiveViewHandle()); } catch (Exception) { return; } startLiveViewBtn.Text = "Stop Live View"; isPreviewRunning_ = true; userInfoGB.Enabled = !isPreviewRunning_; previewSettingsGB.Enabled = !isPreviewRunning_; previewWindow_.Show(this); } else { await stopLiveViewAsync(); isPreviewRunning_ = false; startLiveViewBtn.Text = "Start Live View"; userInfoGB.Enabled = !isPreviewRunning_; previewSettingsGB.Enabled = !isPreviewRunning_; previewWindow_?.ResetLiveView(); previewWindow_?.Dispose(); previewWindow_ = null; } }