WinForms - Make ControlExtensions.InvokeOnUiThreadIfRequired internal

- Additional check is control is being Disposed
- Add copy and paste version of ControlExtensions.InvokeOnUiThreadIfRequired to example project

Resolves #2983
This commit is contained in:
amaitland
2019-12-02 20:37:41 +10:00
parent 30950e5bd1
commit ca2837f928
4 changed files with 65 additions and 8 deletions
@@ -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;
}
/// <summary>
@@ -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)
@@ -88,6 +88,7 @@
<Compile Include="ChromiumContainerControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ControlExtensions.cs" />
<Compile Include="Handlers\DisplayHandler.cs" />
<Compile Include="Handlers\KeyboardHandler.cs" />
<Compile Include="Handlers\LifeSpanHandler.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
{
/// <summary>
/// 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.
/// </summary>
/// <param name="control">the control for which the update is required</param>
/// <param name="action">action to be performed on the control</param>
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();
}
}
}
}
@@ -14,21 +14,30 @@ namespace CefSharp.WinForms.Internals
{
/// <summary>
/// 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.
/// </summary>
/// <param name="control">the control for which the update is required</param>
/// <param name="action">action to be performed on the control</param>
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
{