This is the same issue as I found with SteveSanderson's web window.
I found a race condition and isolated it to line 148 of BlazorWebViewHost.cs. If you insert a Thread.Sleep(2000) you will see it consistently as the task on line 143 does not get through enough initialization before the code on line 160 WebWindow.NavigateToUrl(BlazorAppScheme + "://app/"); gets executed.
i.e. Task.Factory.StartNew(async () =>
{
try
{
var ipc = new IPC(BlazorWebView);
Thread.Sleep(2000);
await RunAsync<TStartup>(ipc, appLifetimeCts.Token);
}
catch (Exception ex)
{
UnhandledException(ex);
throw;
}
});
will prevent the web window from initializing correctly.
This seems to fix the problem. But further investigation is needed
.
.
.
var completed = new ManualResetEventSlim();
CancellationTokenSource appLifetimeCts = new CancellationTokenSource();
Task.Factory.StartNew(async () =>
{
try
{
var ipc = new IPC(BlazorWebView);
Thread.Sleep(2000);
await RunAsync<TStartup>(ipc, appLifetimeCts.Token, completed);
}
catch (Exception ex)
{
UnhandledException(ex);
throw;
}
});
try
{
completed.Wait();
WebWindow.NavigateToUrl(BlazorAppScheme + "://app/");
WebWindow.WaitForExit();
}
finally
{
appLifetimeCts.Cancel();
}
}
.....
private static async Task RunAsync(IPC ipc, CancellationToken appLifetime, ManualResetEventSlim completed)
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true);
JSRuntime = new PlatformJSRuntime(ipc);
completed.Set();
await PerformHandshakeAsync(ipc);
The ManualResetEventSlim will prevent the WebWindow.NavigateToUrl from running until DesktopJSRuntime has been initialized. This seems to fix the problem but I'm not sure if there are other issues or not with the race between NavigateToUrl and RunAsync.
I found this because I am re implementing IBlazorWebView to be able to open a webview on a remote host and it fails every time.