diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 61a01d001..43a0ee16f 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -12,7 +12,6 @@ using CefSharp.Example; using CefSharp.Example.Handlers; using CefSharp.Example.JavascriptBinding; using CefSharp.WinForms.Example.Handlers; -using CefSharp.WinForms.Internals; namespace CefSharp.WinForms.Example { @@ -93,8 +92,9 @@ namespace CefSharp.WinForms.Example CefExample.RegisterTestResources(browser); - var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion); - DisplayOutput(version); + var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion); + //Set label directly, don't use DisplayOutput as call would be a NOOP (no valid handle yet). + outputLabel.Text = version; } /// @@ -332,7 +332,7 @@ namespace CefSharp.WinForms.Example private void DisplayOutput(string output) { - this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output); + outputLabel.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output); } private void HandleToolStripLayout(object sender, LayoutEventArgs e) diff --git a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj index f31773318..e3f0772b7 100644 --- a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj +++ b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj @@ -88,6 +88,7 @@ Component + diff --git a/CefSharp.WinForms.Example/ControlExtensions.cs b/CefSharp.WinForms.Example/ControlExtensions.cs new file mode 100644 index 000000000..9c704a5b6 --- /dev/null +++ b/CefSharp.WinForms.Example/ControlExtensions.cs @@ -0,0 +1,47 @@ +// Copyright © 2019 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Windows.Forms; + +namespace CefSharp.WinForms.Example +{ + public static class ControlExtensions + { + /// + /// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread. + /// No action will be performed if the control doesn't have a valid handle or the control is Disposed/Disposing. + /// + /// the control for which the update is required + /// action to be performed on the control + public static void InvokeOnUiThreadIfRequired(this Control control, Action action) + { + //See https://stackoverflow.com/questions/1874728/avoid-calling-invoke-when-the-control-is-disposed + //for background and some guidance when implementing your own version. + //No action + if (control.Disposing || control.IsDisposed || !control.IsHandleCreated) + { + return; + } + + if (control.InvokeRequired) + { + control.BeginInvoke((Action)(() => + { + //No action + if (control.Disposing || control.IsDisposed || !control.IsHandleCreated) + { + return; + } + + action(); + })); + } + else + { + action.Invoke(); + } + } + } +} diff --git a/CefSharp.WinForms/Internals/ControlExtensions.cs b/CefSharp.WinForms/Internals/ControlExtensions.cs index 7bc7e490b..f367aa150 100644 --- a/CefSharp.WinForms/Internals/ControlExtensions.cs +++ b/CefSharp.WinForms/Internals/ControlExtensions.cs @@ -14,21 +14,30 @@ namespace CefSharp.WinForms.Internals { /// /// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread. - /// No action will be performed if the control doesn't have a valid handle or the control has been disposed. + /// No action will be performed if the control doesn't have a valid handle or the control is Disposed/Disposing. /// /// the control for which the update is required /// action to be performed on the control - public static void InvokeOnUiThreadIfRequired(this Control control, Action action) + internal static void InvokeOnUiThreadIfRequired(this Control control, Action action) { //No action - if (control.IsDisposed || !control.IsHandleCreated) + if (control.Disposing || control.IsDisposed || !control.IsHandleCreated) { return; } if (control.InvokeRequired) { - control.BeginInvoke(action); + control.BeginInvoke((Action)(() => + { + //No action + if (control.Disposing || control.IsDisposed || !control.IsHandleCreated) + { + return; + } + + action(); + })); } else {