void CloseWpfApartment() { if (_wpfApartment != null) { _wpfApartment.Dispose(); _wpfApartment = null; } }
// IComObject methods public void Open() { if (_wpfApartment != null) { throw new InvalidOperationException(); } // start a new thread with WPF Dispatcher _wpfApartment = new WpfApartment(); // attach the input queue of the current thread to that of c var thisThreadId = NativeMethods.GetCurrentThreadId(); _wpfApartment.Invoke(() => NativeMethods.AttachThreadInput(thisThreadId, NativeMethods.GetCurrentThreadId(), true)); // create an instance of BrowserWindow on the WpfApartment's thread _browserWindow = _wpfApartment.Invoke(() => new BrowserWindow(_eventHelper) { Left = 200, Top = 200, Width = 640, Height = 480 }); _wpfApartment.Invoke(() => _browserWindow.Initialize()); }