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

[API Proposal]: Allow thread attributes in CoreLib to be applied at the assembly level. #55978

Closed
AraHaan opened this issue Jul 20, 2021 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.InteropServices
Milestone

Comments

@AraHaan
Copy link
Member

AraHaan commented Jul 20, 2021

Background and motivation

From dotnet/designs#223 (comment) all the way down to the last comment, currently RussKie is considering to do dotnet/designs#223 (comment) which is stated by @jkotas and others as being not pretty and not efficient as then the COM would then be initialized as MTA, then uninitialized, then initialized again as STA. The issue with this stems from the updated code where now you can use top level statements and skip making the Program class and the Main function yourself (where those statements would be inserted inside of an compiler generated Main function. However the compiler does not set any thread apartment attributes by default due to issues that @jkotas also stated in a reply to me about why it cant be generated by the compiler for the Main functions it generates for top level statements for all types of applications as stated here dotnet/designs#223 (comment).

API Proposal

namespace System
{
-   [AttributeUsage(AttributeTargets.Method)]
+   [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method)]
    public sealed class STAThreadAttribute : Attribute
    {
        public STAThreadAttribute()
        {
        }
    }

-   [AttributeUsage(AttributeTargets.Method)]
+   [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method)]
    public sealed class MTAThreadAttribute : Attribute
    {
        public MTAThreadAttribute()
        {
        }
    }
}

API Usage

As for their usage, they can be source generated by the .NET SDK:

For example the normal .NET SDK's (UseWinForms, and UseWPF is not set or set to false) it would generate:

[assembly: MTAThread]

However when one of those properties are set the .NET SDK can instead generate:

[assembly: STAThread]

Risks

The risk should be minimal as the .NET SDK would use this change to generate code that uses the attributes at the assembly level for the user. However it is needed in .NET 6 with those Windows Forms design changes that was merged in. This is to avoid those inefficient workarounds that would then be struck in the LTS release and it would produce more problems than good to just do this change to avoid said hacks for the UI frameworks (WPF and WinForms) to use COM properly.

@AraHaan AraHaan added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 20, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jul 20, 2021
@AraHaan AraHaan changed the title [API Proposal]: Allow thread attributes in CoreLib to be applied to the assembly level. [API Proposal]: Allow thread attributes in CoreLib to be applied at the assembly level. Jul 20, 2021
AraHaan added a commit to AraHaan/runtime that referenced this issue Jul 20, 2021
@jkotas
Copy link
Member

jkotas commented Jul 20, 2021

I have also said: The argument for solving the problem using a language feature has been that there are other attributes that one may want to apply to Main method.

When we had an offline discussion about the right way to address this, we have been leaning towards the language feature since it is more general solution.

@jkoritzinsky
Copy link
Member

Triaging this at 7.0.0 for now since we want to have a conversation with the C# team about a possible language-level solution to this problem.

@jkoritzinsky jkoritzinsky added this to the 7.0.0 milestone Jul 26, 2021
@jkoritzinsky jkoritzinsky removed the untriaged New issue has not been triaged by the area owner label Jul 26, 2021
@jkotas
Copy link
Member

jkotas commented Aug 13, 2021

Created dotnet/csharplang#5045 for the language feature proposal.

@jkotas jkotas closed this as completed Aug 13, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.InteropServices
Projects
None yet
Development

No branches or pull requests

3 participants