private void CaptureLoop(DXGIManager manager, System.Windows.Forms.Control controlToDraw) { _dataStream = new DataStream(_clipRect.Width * _clipRect.Height * 4, true, true); while (true) { int signalId = EventWaitHandle.WaitAny(_waitSignals, Timeout.Infinite); if (signalId == 0) { break; } if (manager.Capture(copyFrameBuffer, 1000) == true) { SharpDX.Direct2D1.Bitmap bitmap = _renderTarget.CreateBitmap(_dataStream); if (bitmap != null) { _queue.Add(bitmap); } } if (controlToDraw.Created) { controlToDraw.BeginInvoke((Action)(() => this.DrawToWindow())); } } }
private void captureThreadFunc(object arg) { DXGIManager manager = null; System.Windows.Forms.Control controlToDraw = arg as System.Windows.Forms.Control; while (true) { using (manager = new DXGIManager(_captureSource)) { if (manager.Initialized == false) { Thread.Sleep(500); continue; } // 이 코드에서 controlToDraw.Handle을 사용하면 "Cross-thread operation not valid" 오류 발생 _renderTarget.Initialize(_drawingWindowHandle, _clipRect.Width, _clipRect.Height); // Initialize 호출 후 너무 빠르게 Capture를 호출하면 (검은 색의) 빈 화면이 나올 수 있음. // 필요하다면 Sleep을 호출 // Thread.Sleep(500); try { CaptureLoop(manager, controlToDraw); break; } catch (SharpDXException e) { if (e.ResultCode != DXGIError.DXGI_ERROR_ACCESS_LOST) { // 예를 들어, Ctrl + Shift + Alt 키를 눌러 데스크탑 전환을 한 경우 manager.Dispose(); } } } } }