Skip to content

Fix ThreadContext CurrentForm to reflect ApplicationContext MainForm changes#14724

Open
abineshPalanisamy wants to merge 4 commits into
dotnet:mainfrom
abineshPalanisamy:Fix_Issue_3260
Open

Fix ThreadContext CurrentForm to reflect ApplicationContext MainForm changes#14724
abineshPalanisamy wants to merge 4 commits into
dotnet:mainfrom
abineshPalanisamy:Fix_Issue_3260

Conversation

@abineshPalanisamy

@abineshPalanisamy abineshPalanisamy commented Jul 10, 2026

Copy link
Copy Markdown

Fixes #3260

Proposed changes

  • Update Application.ThreadContext.CurrentForm to resolve from ApplicationContext.MainForm when no temporary form override is active.
  • Add a temporary current-form override for non-main message loops.
  • Restore the previous current-form override after nested or modal message loops complete.
  • Ensure CurrentForm reflects changes to ApplicationContext.MainForm without requiring explicit synchronization from ApplicationContext.
  • Preserve modal dialog behavior by allowing modal loops to temporarily override CurrentForm.
  • Add regression test coverage for CurrentForm updates when ApplicationContext.MainForm changes.

Root cause

  • ApplicationContext.MainForm is allowed to change at runtime.
  • Application.ThreadContext maintained a cached CurrentForm reference.
  • CurrentForm could become stale after ApplicationContext.MainForm was reassigned.
  • Because of this, internal message-loop logic could continue using the old form.

Customer Impact

  • Ensures ThreadContext.CurrentForm reflects the latest ApplicationContext.MainForm.
  • Prevents stale form references after MainForm is changed.
  • Improves correctness for applications that dynamically replace their main form.
  • Avoids incorrect behavior when the old main form is closed after switching to a new main form.
  • Preserves modal dialog behavior by keeping temporary modal form overrides intact.

Regression?

  • No

Risk

  • Low.
  • The change is internal to Application.ThreadContext.
  • No public API behavior is changed.
  • ApplicationContext.MainForm continues to behave as documented.
  • Modal-loop behavior is preserved through temporary current-form overrides.

Screenshots

  • Not applicable. No UI rendering changes.

Test methodology

  • Reproduced the issue by changing ApplicationContext.MainForm after the message loop was initialized.
  • Verified that before the fix:
    • ApplicationContext.MainForm changed to the new form,
    • ThreadContext.CurrentForm continued to reference the old form.
  • Verified that after the fix:
    • ThreadContext.CurrentForm reflects the updated ApplicationContext.MainForm.
    • Setting ApplicationContext.MainForm to null is reflected by CurrentForm.
    • Multiple MainForm changes are reflected correctly.
    • Modal form override behavior is preserved.
  • Added unit test coverage for:
    • ThreadContext_CurrentForm_ReflectsChangedMainForm
    • ThreadContext_CurrentForm_ReflectsNullMainForm
    • ThreadContext_CurrentForm_ReflectsMultipleMainFormChanges
    • ThreadContext_CurrentForm_OverrideTakesPriorityOverMainForm

Accessibility testing

Test environment(s)

  • 11.0.100-preview.3.26170.106
Microsoft Reviewers: Open in CodeFlow

Added fix for the Fix_Issue_3260
Addressed the review correction and updated the test cases
Updated the changes for Fix_Issue_3260
Added the unit test cases for Fix_Issue_3260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing MainForm doesn't update reference in ThreadContext

1 participant