public unsafe MainWindow() { InitializeComponent(); InitializeServer(); this.Closing += new CancelEventHandler(OnWindowClosing); System.Windows.Threading.Dispatcher uiDispatcher = this.Dispatcher; UtilMPipeline pp = new UtilMPipeline(); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate(object s, DoWorkEventArgs args) { pp.EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_DEPTH); pp.Init(); for (; ; ) { if (!pp.AcquireFrame(true)) break; { PXCMImage image = pp.QueryImage(PXCMImage.ImageType.IMAGE_TYPE_DEPTH); PXCMImage.ImageData ddata; pxcmStatus sts = image.AcquireAccess(PXCMImage.Access.ACCESS_READ, out ddata); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) break; short* depth = (short*)ddata.buffer.planes[0]; short minZ = 32001; int minX = 0; int minY = 0; short maxX = 320; for (int y = 0; y < 240; y++) { for (int x = 0; x < 320; x++) { if (depth[(y * 320) + x] < minZ) { minZ = depth[(y * 320) + x]; minX = x; minY = y; } } } ///////////////////// //normalize extremes// if (minZ < DistanceFromScreenEdge) minZ = DistanceFromScreenEdge; //match origin// minX -= 160; minY -= 120; ///////////////////// maxX = (short)(Math.Tan(FOV / 2) * minZ); short discardedX = (short)(Math.Tan(FOV / 2) * (minZ - DistanceFromScreenEdge)); short screenMaxX = (short)(maxX - discardedX); float xRatio = minX / screenMaxX; UpdateGridDelegate update = new UpdateGridDelegate(UpdateGrid); uiDispatcher.BeginInvoke(update, minX, minY, minZ, maxX, discardedX, screenMaxX, xRatio); image.ReleaseAccess(ref ddata); pp.ReleaseFrame(); } } pp.Close(); pp.Dispose(); }; worker.RunWorkerAsync(); }
private void ManageProgress(BindingSource bindingSource, DoubleBufferedDataGridView grid, FrameType frameType, int sleepTimer) { var progress = new Progress(); progress.SetupAndShow(this, 0, 0, false, true, waitHandle); progressSearched = 0; progressFound = 0; UpdateGridDelegate gridUpdater = UpdateGrid; var updateParams = new object[] { bindingSource }; ResortGridDelegate gridSorter = ResortGrid; var sortParams = new object[] { bindingSource, grid, frameType }; ThreadDelegate enableGenerateButton = EnableSeedGenerate; try { bool alive = true; while (alive) { progress.ShowProgress(progressSearched / (float)progressTotal, progressSearched, progressFound); if (refreshQueue) { Invoke(gridUpdater, updateParams); refreshQueue = false; } if (jobs != null) { foreach (Thread job in jobs) { if (job != null && job.IsAlive) { alive = true; break; } alive = false; } } if (sleepTimer > 0) { Thread.Sleep(sleepTimer); } } } catch (ObjectDisposedException) { // This keeps the program from crashing when the Time Finder progress box // is closed from the Windows taskbar. } catch (Exception exception) { if (exception.Message != "Operation Cancelled") { throw; } } finally { progress.Finish(); if (jobs != null) { for (int i = 0; i < jobs.Length; i++) { if (jobs[i] != null) { jobs[i].Abort(); } } } Invoke(enableGenerateButton); Invoke(gridSorter, sortParams); } }