Skip to content

Commit fcbdda0

Browse files
committed
WinForms - Add some extra Dispose checks to InvokeOnUiThreadIfRequired
Also added stackoverflow link regarding calling after the control has been disposed
1 parent 0d386bf commit fcbdda0

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

CefSharp.MinimalExample.WinForms/Controls/ControlExtensions.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@
77

88
namespace CefSharp.MinimalExample.WinForms.Controls
99
{
10-
public static class ControlExtensions
11-
{
12-
/// <summary>
13-
/// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread.
14-
/// </summary>
15-
/// <param name="control">the control for which the update is required</param>
16-
/// <param name="action">action to be performed on the control</param>
17-
public static void InvokeOnUiThreadIfRequired(this Control control, Action action)
18-
{
19-
if (control.InvokeRequired)
20-
{
21-
control.BeginInvoke(action);
22-
}
23-
else
24-
{
25-
action.Invoke();
26-
}
27-
}
28-
}
10+
public static class ControlExtensions
11+
{
12+
/// <summary>
13+
/// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread.
14+
/// </summary>
15+
/// <param name="control">the control for which the update is required</param>
16+
/// <param name="action">action to be performed on the control</param>
17+
public static void InvokeOnUiThreadIfRequired(this Control control, Action action)
18+
{
19+
//If you are planning on using a similar function in your own code then please be sure to
20+
//have a quick read over https://stackoverflow.com/questions/1874728/avoid-calling-invoke-when-the-control-is-disposed
21+
//No action
22+
if (control.Disposing || control.IsDisposed || !control.IsHandleCreated)
23+
{
24+
return;
25+
}
26+
27+
if (control.InvokeRequired)
28+
{
29+
control.BeginInvoke(action);
30+
}
31+
else
32+
{
33+
action.Invoke();
34+
}
35+
}
36+
}
2937
}

0 commit comments

Comments
 (0)