From 60441d49aeb3158714ca5c9fd1f75a46bf5b5abb Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Wed, 27 May 2026 11:11:19 +0800 Subject: [PATCH 1/2] Handle transient GDI+ ExternalException in SplitContainer.RepaintSplitterRect during display-session transition --- .../Forms/Layout/Containers/SplitContainer.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs b/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs index dbb1dded2f1..4a88792dadb 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs @@ -1550,16 +1550,24 @@ private void RepaintSplitterRect() { if (IsHandleCreated) { - using Graphics g = CreateGraphicsInternal(); - if (BackgroundImage is not null) + try { - using TextureBrush textureBrush = new(BackgroundImage, WrapMode.Tile); - g.FillRectangle(textureBrush, ClientRectangle); + using Graphics g = CreateGraphicsInternal(); + if (BackgroundImage is not null) + { + using TextureBrush textureBrush = new(BackgroundImage, WrapMode.Tile); + g.FillRectangle(textureBrush, ClientRectangle); + } + else + { + using var solidBrush = BackColor.GetCachedSolidBrushScope(); + g.FillRectangle(solidBrush, _splitterRect); + } } - else + catch (ExternalException) { - using var solidBrush = BackColor.GetCachedSolidBrushScope(); - g.FillRectangle(solidBrush, _splitterRect); + // GDI+ can transiently fail while the display session is transitioning (for example, lock/unlock). + // Ignore repaint failures here and let normal painting recover once graphics resources are available. } } } From 4e84aa1d37b9dc6a24476ed022c3d9d361086245 Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Thu, 28 May 2026 14:40:15 +0800 Subject: [PATCH 2/2] Add Debug.Fail for ExternalException to prevent silent failures --- .../System/Windows/Forms/Layout/Containers/SplitContainer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs b/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs index 4a88792dadb..5fca905c8d4 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Layout/Containers/SplitContainer.cs @@ -1564,10 +1564,11 @@ private void RepaintSplitterRect() g.FillRectangle(solidBrush, _splitterRect); } } - catch (ExternalException) + catch (ExternalException ex) { // GDI+ can transiently fail while the display session is transitioning (for example, lock/unlock). // Ignore repaint failures here and let normal painting recover once graphics resources are available. + Debug.Fail($"{nameof(RepaintSplitterRect)} failed with hr={ex.ErrorCode}, message={ex}"); } } }