Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CuteLeon opened this issue Aug 23, 2024 · 5 comments · May be fixed by #11972
Open

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

CuteLeon opened this issue Aug 23, 2024 · 5 comments · May be fixed by #11972
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation 🚧 work in progress Work that is current in progress untriaged The team needs to look at this issue in the next triage

Comments

@CuteLeon
Copy link
Contributor

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;

@CuteLeon CuteLeon added api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation untriaged The team needs to look at this issue in the next triage labels Aug 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the 🚧 work in progress Work that is current in progress label Aug 23, 2024
@CuteLeon
Copy link
Contributor Author

CuteLeon commented Aug 26, 2024

Workaround:

var currentThreadContext = typeof(WinApplication).Assembly
    .GetType("System.Windows.Forms.Application+ThreadContext")
    .GetMethod("FromCurrent", BindingFlags.Static | BindingFlags.NonPublic)
    .Invoke(default, default);
var currentApplicationContext = currentThreadContext
    .GetType()
    .GetProperty("ApplicationContext")
    .GetValue(currentThreadContext) as ApplicationContext;
var mainForm = currentApplicationContext.MainForm;
MessageBox.Show(mainForm, "Message content");

@JeremyKuhne
Copy link
Member

To be safer it would probably be better to just expose the MainForm on Application (as a getter only). Would this work for you @CuteLeon?

@JeremyKuhne JeremyKuhne added 📭 waiting-author-feedback The team requires more information from the author and removed untriaged The team needs to look at this issue in the next triage labels Sep 10, 2024
@CuteLeon
Copy link
Contributor Author

yes, it works for me as I only desire MainForm on current thread.
PR updated.

@dotnet-policy-service dotnet-policy-service bot added untriaged The team needs to look at this issue in the next triage and removed 📭 waiting-author-feedback The team requires more information from the author labels Sep 11, 2024
@CuteLeon
Copy link
Contributor Author

CuteLeon commented Oct 11, 2024

Hi @JeremyKuhne, would you please help to have a review on pr #11972 ?

@JeremyKuhne
Copy link
Member

I’ll look as soon as I am able. I’ll also need to take this through API review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation 🚧 work in progress Work that is current in progress untriaged The team needs to look at this issue in the next triage
Projects
None yet
2 participants