/// <summary> /// Initializes CefSharp with user-provided settings. This method allows you to wait for /// <see cref="IBrowserProcessHandler.OnContextInitialized"/> to be called before continuing. /// It's important to note that Initialize and Shutdown <strong>MUST</strong> be called on your main /// application thread (typically the UI thread). If you call them on different /// threads, your application will hang. See the documentation for Cef.Shutdown() for more details. /// </summary> /// <param name="settings">CefSharp configuration settings.</param> /// <param name="performDependencyCheck">Check that all relevant dependencies available, throws exception if any are missing</param> /// <param name="browserProcessHandler">The handler for functionality specific to the browser process. Null if you don't wish to handle these events</param> /// <returns>returns a Task that can be awaited. true if successful; otherwise, false. If false check the log file for possible errors</returns> /// <remarks> /// If successful then the Task will be completed successfully when <see cref="IBrowserProcessHandler.OnContextInitialized"/> is called. /// If successful then the continuation will happen syncrionously on the CEF UI thread. /// </remarks> public static Task <bool> InitializeAsync(CefSettingsBase settings, bool performDependencyCheck = true, IBrowserProcessHandler browserProcessHandler = null) { using (settings.settings) { try { //Ignore the result, the Task will be set in Core.Cef.Initialze Core.Cef.Initialize(settings.settings, performDependencyCheck, browserProcessHandler); } catch (Exception ex) { GlobalContextInitialized.SetException(ex); } } return(GlobalContextInitialized.Task); }
/// <summary> /// Create the underlying browser. The instance address, browser settings and request context will be used. /// </summary> /// <param name="windowInfo">Window information used when creating the browser</param> /// <param name="browserSettings">Browser initialization settings</param> /// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception> public void CreateBrowser(IWindowInfo windowInfo = null, IBrowserSettings browserSettings = null) { if (browserCreated) { throw new Exception("An instance of the underlying offscreen browser has already been created, this method can only be called once."); } browserCreated = true; if (browserSettings == null) { browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true); } if (windowInfo == null) { windowInfo = Core.ObjectFactory.CreateWindowInfo(); windowInfo.SetAsWindowless(IntPtr.Zero); } //TODO: We need some sort of timeout and //if we use the same approach for WPF/WinForms then //we need to move the common code into the partial class GlobalContextInitialized.ExecuteOrEnqueue((success) => { if (!success) { return; } managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, RequestContext, Address); //Dispose of BrowserSettings if we created it, if user created then they're responsible if (browserSettings.AutoDispose) { browserSettings.Dispose(); } browserSettings = null; }); }