From 023e65ccdabed1e6225a4619489617bf27c0bd14 Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Wed, 1 Jul 2020 13:01:08 +1000 Subject: [PATCH 1/2] Refactor Animate method --- .../src/System/Windows/Forms/ToolStripItem.cs | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs index 50ca0194c88..3c53e65634b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs @@ -2056,25 +2056,27 @@ private void Animate() private void Animate(bool animate) { - if (animate != _state[s_stateCurrentlyAnimatingImage]) + if (animate == _state[s_stateCurrentlyAnimatingImage]) { - if (animate) - { - if (Image != null) - { - ImageAnimator.Animate(Image, new EventHandler(OnAnimationFrameChanged)); - _state[s_stateCurrentlyAnimatingImage] = animate; - } - } - else - { - if (Image != null) - { - ImageAnimator.StopAnimate(Image, new EventHandler(OnAnimationFrameChanged)); - _state[s_stateCurrentlyAnimatingImage] = animate; - } - } + return; } + + Image image = Image; + if (image == null) + { + return; + } + + if (animate) + { + ImageAnimator.Animate(image, new EventHandler(OnAnimationFrameChanged)); + } + else + { + ImageAnimator.StopAnimate(image, new EventHandler(OnAnimationFrameChanged)); + } + + _state[s_stateCurrentlyAnimatingImage] = animate; } internal bool BeginDragForItemReorder() From 9ef77b6e01b87cdb0014cab2b79c2d6ad855987d Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Wed, 1 Jul 2020 13:21:39 +1000 Subject: [PATCH 2/2] Prevent ImageList allocation disposing PropertyGrid Resolves #3485 --- .../System/Windows/Forms/ImageList.NativeImageList.cs | 11 +++++++++-- .../src/System/Windows/Forms/ToolStripItem.cs | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ImageList.NativeImageList.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ImageList.NativeImageList.cs index 713ba71c2ba..ebd42c2d4fe 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ImageList.NativeImageList.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ImageList.NativeImageList.cs @@ -1,9 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #nullable disable +using System.Diagnostics; using static Interop; namespace System.Windows.Forms @@ -42,7 +43,13 @@ public void Dispose(bool disposing) } } - ~NativeImageList() => Dispose(false); + ~NativeImageList() + { +#if DEBUG + Debug.Fail($"{nameof(NativeImageList)} was not disposed properly. Originating stack:\n{_callStack}"); +#endif + Dispose(false); + } } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs index 3c53e65634b..74d0ff00ede 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripItem.cs @@ -1018,7 +1018,7 @@ public virtual Image Image get { Image image = (Image)Properties.GetObject(s_imageProperty); - if (image == null && (Owner != null) && (Owner.ImageList != null) && ImageIndexer.ActualIndex >= 0) + if (image == null && Owner?.ImageList?.HandleCreated == true && ImageIndexer.ActualIndex >= 0) { if (ImageIndexer.ActualIndex < Owner.ImageList.Images.Count) {