Skip to content

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

Merged
LeafShi1 merged 2 commits into
dotnet:mainfrom
LeafShi1:Fix_14562_GDI+_Exception_drawing_SplitContainer
Jul 1, 2026
Merged

LeafShi1 merged 2 commits into
dotnet:mainfrom
LeafShi1:Fix_14562_GDI+_Exception_drawing_SplitContainer

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented May 27, 2026 •

Copy link
Copy Markdown
Member

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Jun 9, 2026
@LeafShi1 LeafShi1 added this to the 11.0-preview7 milestone Jul 1, 2026
@LeafShi1
LeafShi1 merged commit e37e3c6 into dotnet:main Jul 1, 2026
8 checks passed
@dotnet-policy-service dotnet-policy-service Bot removed the waiting-review This item is waiting on review by one or more members of team label Jul 1, 2026
@LeafShi1 LeafShi1 added the releasenotes-candidate This issue is a candidate to be mentioned in the release notes, if the respective PR fixed it. label Jul 15, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

needs-area-label releasenotes-candidate This issue is a candidate to be mentioned in the release notes, if the respective PR fixed it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GDI+ Exception drawing SplitContainer when PC Locked

5 participants