Skip to content

Enhance WinForms Application static class to get current application context #11971

Open
@CuteLeon

Description

Background and motivation

I have a complex winform application and made multiple forms run message loops on independent threads, I registered ThreadException on each form's thread, and hope to get current form thread's application context in ThreadException event callback, to show a dialog with current application context's MainForm as dialog's owner (ensure alert shows in front of current thread's main form).

API Proposal

namespace System.Windows.Forms;

public sealed partial class Application
{
    public static ApplicationContext GetCurrentApplicationContext()
        => ThreadContext.FromCurrent().ApplicationContext;
}

API Usage

var owner = Application.GetCurrentApplicationContext().MainForm;

Alternative Designs

No response

Risks

Add a new method, no clearly risk.

Will this feature affect UI controls?

No;
No;
No;

Metadata

Assignees

Labels

🚧 work in progressWork that is current in progressapi-suggestion(1) Early API idea and discussion, it is NOT ready for implementation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions