Search code examples
wpfcefsharp

Open modal dialog in WPF to get credentials for CefSharp


I have a WPF application which is hosting a conversation window from skype for business; the window can open an child control, a Chromiun web browser (CefSharp). The browser opens a site that needs credentials, so the method GetAuthCredentials is override in order to process the request. The code goes like this:

AuthBox dlg = new AuthBox(); 
dlg.Owner = _parent;
dlg.ShowDialog();
callback.Continue(dlg.Username, dlg.Password);

The problem is: I need to make this dialog as modal, in order to lock the parent window (the conversation); for that reason, dlg.Owner = _parent; is added. However this throws an exception: System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'

To use dispatcher.BeginInvoke doesn't work. Is there any other way to open the login window (modal) avoiding this threading issue?


Solution

  • I assume dispatcher.BeginInvoke doesn't work for you because it doesn't block? Try using a synchronization primitive, such as ManualResetEvent, open the dialog in UI thread, in calling thread wait for ManualResetEvent notification, and make sure dialog raises the event when done.