diff --git a/MobiFlight/BrowserMessages/Publisher/PostMessagePublisher.cs b/MobiFlight/BrowserMessages/Publisher/PostMessagePublisher.cs index 0e4e77a68..0d245a085 100644 --- a/MobiFlight/BrowserMessages/Publisher/PostMessagePublisher.cs +++ b/MobiFlight/BrowserMessages/Publisher/PostMessagePublisher.cs @@ -1,15 +1,16 @@ using Microsoft.Web.WebView2.Core; using Newtonsoft.Json; using System; +using System.Threading; namespace MobiFlight.BrowserMessages.Publisher { public class PostMessagePublisher : IMessagePublisher { - private readonly CoreWebView2 _webView; + private readonly ThreadSafeWebView2 _webView; private Action _onMessageReceived; - public PostMessagePublisher(CoreWebView2 webView) + public PostMessagePublisher(ThreadSafeWebView2 webView) { _webView = webView; _webView.WebMessageReceived += WebView_WebMessageReceived; @@ -21,7 +22,8 @@ public void Publish(TEvent eventToPublish) { var message = new Message() { key = eventToPublish.GetType().Name, payload = eventToPublish }; var jsonMessage = JsonConvert.SerializeObject(message); - _webView.PostWebMessageAsJson(jsonMessage); + // Ensure the call is made on the UI thread + _webView.PostWebMessageAsJsonThreadSafe(jsonMessage); } } diff --git a/MobiFlight/ThreadSafeWebView2.cs b/MobiFlight/ThreadSafeWebView2.cs new file mode 100644 index 000000000..450043a54 --- /dev/null +++ b/MobiFlight/ThreadSafeWebView2.cs @@ -0,0 +1,20 @@ +using Microsoft.Web.WebView2.WinForms; +using System; + +namespace MobiFlight +{ + public class ThreadSafeWebView2 : WebView2 + { + public void PostWebMessageAsJsonThreadSafe(string jsonMessage) + { + if (this.InvokeRequired) + { + this.Invoke(new Action(() => CoreWebView2.PostWebMessageAsJson(jsonMessage))); + } + else + { + CoreWebView2.PostWebMessageAsJson(jsonMessage); + } + } + } +} diff --git a/MobiFlightConnector.csproj b/MobiFlightConnector.csproj index ba1469d3f..119b623f7 100644 --- a/MobiFlightConnector.csproj +++ b/MobiFlightConnector.csproj @@ -370,6 +370,9 @@ + + Component + diff --git a/UI/Panels/FrontendPanel.Designer.cs b/UI/Panels/FrontendPanel.Designer.cs index 1986e803c..8497d059d 100644 --- a/UI/Panels/FrontendPanel.Designer.cs +++ b/UI/Panels/FrontendPanel.Designer.cs @@ -28,7 +28,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.FrontendWebView = new Microsoft.Web.WebView2.WinForms.WebView2(); + this.FrontendWebView = new ThreadSafeWebView2(); ((System.ComponentModel.ISupportInitialize)(this.FrontendWebView)).BeginInit(); this.SuspendLayout(); // @@ -58,6 +58,6 @@ private void InitializeComponent() #endregion - private Microsoft.Web.WebView2.WinForms.WebView2 FrontendWebView; + private ThreadSafeWebView2 FrontendWebView; } } diff --git a/UI/Panels/FrontendPanel.cs b/UI/Panels/FrontendPanel.cs index 25250e216..3b466936f 100644 --- a/UI/Panels/FrontendPanel.cs +++ b/UI/Panels/FrontendPanel.cs @@ -47,7 +47,7 @@ async void InitializeAsync() FrontendWebView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; FrontendWebView.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded; - MessageExchange.Instance.SetPublisher(new PostMessagePublisher(FrontendWebView.CoreWebView2)); + MessageExchange.Instance.SetPublisher(new PostMessagePublisher(FrontendWebView)); } private void CoreWebView2_DOMContentLoaded(object sender, CoreWebView2DOMContentLoadedEventArgs e)