diff --git a/src/Febe.Desktop/Browser/WebView2Browser.cs b/src/Febe.Desktop/Browser/WebView2Browser.cs index f717b11..a68effc 100644 --- a/src/Febe.Desktop/Browser/WebView2Browser.cs +++ b/src/Febe.Desktop/Browser/WebView2Browser.cs @@ -68,10 +68,6 @@ internal sealed class WebView2Browser : IFebeBrowser try { await Host.StartAsync(_options, ct).ConfigureAwait(false); - _started = true; - _lastError = null; - _context.Log.Info("browser", $"Browser di Febe pronto: WebView2 {Host.Version}, pagina {WindowWidth.ToString(CultureInfo.InvariantCulture)}×{WindowHeight.ToString(CultureInfo.InvariantCulture)}, {BrowserHostingNames.Name(_settings.Hosting)}, fuori vista."); - return; } catch (OperationCanceledException) when (ct.IsCancellationRequested) { @@ -91,7 +87,16 @@ internal sealed class WebView2Browser : IFebeBrowser { return; } + + continue; } + + // Outside the retries: what follows a start that worked never undoes it. This runs on a thread of the + // composition, so it reads only plain values of the host (the version was read on the UI thread). + _started = true; + _lastError = null; + _context.Log.Info("browser", $"Browser di Febe pronto: WebView2 {Host.Version}, pagina {WindowWidth.ToString(CultureInfo.InvariantCulture)}×{WindowHeight.ToString(CultureInfo.InvariantCulture)}, {BrowserHostingNames.Name(_settings.Hosting)}, fuori vista."); + return; } } diff --git a/src/Febe.Desktop/Browser/WebView2Host.cs b/src/Febe.Desktop/Browser/WebView2Host.cs index 22d4b95..8fe5919 100644 --- a/src/Febe.Desktop/Browser/WebView2Host.cs +++ b/src/Febe.Desktop/Browser/WebView2Host.cs @@ -57,6 +57,9 @@ internal sealed class WebView2Host : IBrowserHost private bool _closing; private bool _restarting; + // Read on the UI thread during the start: a plain value that any thread may read. + private volatile string? _version; + public WebView2Host(Dispatcher ui, WebView2HostOptions settings, FebeLog log) { _ui = ui ?? throw new ArgumentNullException(nameof(ui)); @@ -79,8 +82,11 @@ internal sealed class WebView2Host : IBrowserHost /// True while a WebView is attached (commands can be sent). public bool IsAttached => _session.IsAttached; - /// The version of the WebView2 runtime in use (for the log). - public string? Version => _environment?.BrowserVersionString; + /// + /// The version of the WebView2 runtime of the browser in use (for the log), read on the UI thread when it started; + /// null before the first start. Any thread: it is a plain value, never a member of a WebView2 object. + /// + public string? Version => _version; /// Raised on the UI thread when the browser process of WebView2 went away by itself (the owner restarts). public event EventHandler? BrowserProcessFailed; @@ -200,6 +206,10 @@ internal sealed class WebView2Host : IBrowserHost _exited = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); _environment.BrowserProcessExited += OnBrowserProcessExited; + // Read here, on the UI thread, for the log line the composition writes from another thread: a member of the + // environment read there throws «CoreWebView2Environment members can only be accessed from the UI thread». + _version = _environment.BrowserVersionString; + var hwnd = _window.Handle; if (_settings.Hosting == BrowserHosting.Composition) {