Skip to content

Handle transient GDI+ ExternalException in SplitContainer.RepaintSplitterRect during display-session transition#14565

Open
LeafShi1 wants to merge 2 commits into
dotnet:mainfrom
LeafShi1:Fix_14562_GDI+_Exception_drawing_SplitContainer
Open

Handle transient GDI+ ExternalException in SplitContainer.RepaintSplitterRect during display-session transition#14565
LeafShi1 wants to merge 2 commits into
dotnet:mainfrom
LeafShi1:Fix_14562_GDI+_Exception_drawing_SplitContainer

Conversation

@LeafShi1
Copy link
Copy Markdown
Member

@LeafShi1 LeafShi1 commented May 27, 2026

Fixes #14562

Root Cause

SplitContainer.RepaintSplitterRect() performs immediate GDI+ drawing during layout (CreateGraphicsInternal + FillRectangle).
During display-session transitions (for example, Windows lock/unlock), the graphics context can become transiently invalid and GDI+ may throw ExternalException (A generic error occurred in GDI+).
This exception was previously unhandled in this path and could bubble up as an unhandled exception.

Proposed changes

  • Add targeted exception handling in SplitContainer.RepaintSplitterRect():
    • Wrap current repaint logic in try/catch (ExternalException).
    • Keep existing rendering behavior unchanged when drawing succeeds.
    • Ignore only transient GDI+ failures so normal paint/layout cycles can recover automatically.

Customer Impact

  • Users no longer see intermittent crash dialogs (“A generic error occurred in GDI+”) when lock/unlock happens during window creation/layout in forms that use SplitContainer

Regression?

  • No

Risk

  • Minimal

Screenshots

Sample project: WinFormsApp5.zip

Before

When a window containing SplitContainer is opened and the machine is locked before layout completes, unlocking can trigger an unhandled error dialog (“A generic error occurred in GDI+”).

image

After

Under the same lock/unlock timing, users no longer see the GDI+ error dialog and the app does not crash from this path.
The window opens and continues rendering normally after unlock, with no new UI changes in normal scenarios.

image

Test methodology

  • Manually

Test environment(s)

  • .net 11.0.0-preview.5.26272.112
Microsoft Reviewers: Open in CodeFlow

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request prevents intermittent crashes in WinForms SplitContainer during display-session transitions (e.g., Windows lock/unlock) by handling transient GDI+ failures that can occur when repainting the splitter during layout.

Changes:

  • Wrap SplitContainer.RepaintSplitterRect() immediate GDI+ drawing in a try/catch (ExternalException).
  • Suppress transient GDI+ repaint failures so layout/painting can recover on subsequent paint cycles.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GDI+ Exception drawing SplitContainer when PC Locked

3 participants