diff --git a/CefSharp.Core.Runtime/Cef.h b/CefSharp.Core.Runtime/Cef.h index a408dd423..0301d2acf 100644 --- a/CefSharp.Core.Runtime/Cef.h +++ b/CefSharp.Core.Runtime/Cef.h @@ -46,7 +46,7 @@ namespace CefSharp private: static Object^ _sync; - static bool _initialized = false; + static Nullable _initialized; static bool _hasShutdown = false; static HashSet^ _disposables; static int _initializedThreadId; @@ -86,9 +86,9 @@ namespace CefSharp /// Gets a value that indicates whether CefSharp is initialized. /// true if CefSharp is initialized; otherwise, false. - static property bool IsInitialized + static property Nullable IsInitialized { - bool get() + Nullable get() { return _initialized; } @@ -255,7 +255,7 @@ namespace CefSharp /// true if successful; otherwise, false. static bool Initialize(CefSettingsBase^ cefSettings, bool performDependencyCheck, IApp^ cefApp) { - if (_initialized) + if (_initialized.HasValue) { // NOTE: Can only initialize Cef once, to make this explicitly clear throw exception on subsiquent attempts throw gcnew Exception("Cef.Initialize can only be called once per process. This is a limitation of the underlying " + @@ -555,11 +555,11 @@ namespace CefSharp /// static void Shutdown() { - if (_initialized) + if (_initialized.GetValueOrDefault()) { msclr::lock l(_sync); - if (_initialized) + if (_initialized.GetValueOrDefault()) { if (_initializedThreadId != Thread::CurrentThread->ManagedThreadId) { @@ -615,11 +615,11 @@ namespace CefSharp /// static void ShutdownWithoutChecks() { - if (_initialized) + if (_initialized.GetValueOrDefault()) { msclr::lock l(_sync); - if (_initialized) + if (_initialized.GetValueOrDefault()) { CefShutdown(); _initialized = false; @@ -821,7 +821,7 @@ namespace CefSharp return; } - if (_initialized) + if (_initialized.HasValue) { throw gcnew Exception("Must be enabled before Cef.Initialize is called. "); } diff --git a/CefSharp.Core/Cef.cs b/CefSharp.Core/Cef.cs index 1f4eef9ff..533b14493 100644 --- a/CefSharp.Core/Cef.cs +++ b/CefSharp.Core/Cef.cs @@ -50,8 +50,11 @@ namespace CefSharp } /// Gets a value that indicates whether CefSharp is initialized. - /// true if CefSharp is initialized; otherwise, false. - public static bool IsInitialized + /// + /// true if CefSharp is initialized; returns false if initialize failed. + /// null if initialize not yet called. + /// + public static bool? IsInitialized { get { return Core.Cef.IsInitialized; } } diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index be25caa34..7d4e02f24 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -224,15 +224,7 @@ namespace CefSharp.OffScreen IRequestContext requestContext = null, bool automaticallyCreateBrowser = true, Action onAfterBrowserCreated = null, bool useLegacyRenderHandler = true) { - if (!Cef.IsInitialized) - { - var settings = new CefSettings(); - - if (!Cef.Initialize(settings)) - { - throw new InvalidOperationException(CefInitializeFailedErrorMessage); - } - } + InitializeCefInternal(); RequestContext = requestContext; diff --git a/CefSharp.Test/CefSharpFixture.cs b/CefSharp.Test/CefSharpFixture.cs index 275b20bde..8e7c87ff0 100644 --- a/CefSharp.Test/CefSharpFixture.cs +++ b/CefSharp.Test/CefSharpFixture.cs @@ -31,7 +31,7 @@ namespace CefSharp.Test private void CefInitialize() { - if (!Cef.IsInitialized) + if (Cef.IsInitialized == null) { var isDefault = AppDomain.CurrentDomain.IsDefaultAppDomain(); if (!isDefault) @@ -63,11 +63,16 @@ namespace CefSharp.Test diagnosticMessageSink.OnMessage(new DiagnosticMessage("Cef Initialized:" + success)); } + + if (Cef.IsInitialized == false) + { + throw new InvalidOperationException("Cef.Initialize failed."); + } } private void CefShutdown() { - if (Cef.IsInitialized) + if (Cef.IsInitialized == true) { diagnosticMessageSink.OnMessage(new DiagnosticMessage("Before Cef Shutdown")); diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 0d3153146..159e9df76 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -333,10 +333,7 @@ namespace CefSharp.WinForms { if (!initialized) { - if (!Cef.IsInitialized && !Cef.Initialize(new CefSettings())) - { - throw new InvalidOperationException(CefInitializeFailedErrorMessage); - } + InitializeCefInternal(); Cef.AddDisposable(this); diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 8730f88fe..3af233711 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -552,16 +552,7 @@ namespace CefSharp.Wpf [MethodImpl(MethodImplOptions.NoInlining)] private void NoInliningConstructor() { - //Initialize CEF if it hasn't already been initialized - if (!Cef.IsInitialized) - { - var settings = new CefSettings(); - - if (!Cef.Initialize(settings)) - { - throw new InvalidOperationException(CefInitializeFailedErrorMessage); - } - } + InitializeCefInternal(); //Add this ChromiumWebBrowser instance to a list of IDisposable objects // that if still alive at the time Cef.Shutdown is called will be disposed of diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index 8ba735e31..fe974d9a6 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -27,6 +27,7 @@ namespace CefSharp.WinForms "the IsBrowserInitialized property to determine when the browser has been initialized."; private const string CefInitializeFailedErrorMessage = "Cef.Initialize() failed.Check the log file see https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file for details."; + private const string CefIsInitializedFalseErrorMessage = "Cef.IsInitialized was false!.Check the log file for errors!. See https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#log-file for details."; /// /// Used as workaround for issue https://github.com/cefsharp/CefSharp/issues/3021 @@ -506,6 +507,22 @@ namespace CefSharp.WinForms this.FreeDevToolsContext(); } + private static void InitializeCefInternal() + { + if (Cef.IsInitialized == null) + { + if (!Cef.Initialize(new CefSettings())) + { + throw new InvalidOperationException(CefInitializeFailedErrorMessage); + } + } + + if (Cef.IsInitialized == false) + { + throw new InvalidOperationException(CefIsInitializedFalseErrorMessage); + } + } + /// /// Check is browser is initialized ///