private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { DateTime lastSecond = DateTime.Now; int fps = 0; while (!backgroundWorker.CancellationPending) { try { cam.Update(); } catch (Exception ex) { log.Debug("Update failed", ex); continue; } ImageBase camImg = cam.CalcSelectedChannel(); if (null == camImg) { // ignore errors. However, this might be a hint that something is wrong in your application. continue; } if (camImg is FloatImage && (cam.SelectedChannel == ChannelNames.Distance || cam.SelectedChannel == ChannelNames.ZImage)) { TrimImage((FloatImage)camImg, Properties.Settings.Default.MinDepthToDisplay, Properties.Settings.Default.MaxDepthToDisplay); } Bitmap bmp = camImg.ToBitmap(); if (saveSnapshot) { string snapName = "MetriCam 2 Snapshot.png"; string snapFilename = Path.GetTempPath() + snapName; bmp.Save(snapFilename); MessageBox.Show(string.Format("Snapshot saved as '{0}'.", snapFilename), "Snapshot saved", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); saveSnapshot = false; } this.BeginInvokeEx(f => pictureBox.Image = bmp); fps++; DateTime now = DateTime.Now; if (now - lastSecond > new TimeSpan(0, 0, 1)) { int fpsCopy = fps; this.BeginInvokeEx(f => labelFps.Text = $"{fpsCopy} fps"); lastSecond = now; fps = 0; } } DisconnectCamera(); isBgwFinished.Set(); }