Skip to content

[API Proposal]: Dispose Application in Generated Entry Point #10074

Open
@koenigst

Description

@koenigst

Background and motivation

If any disposable fields are present in the application (i.e. App.xaml.cs) the default analyzers will suggests implementing IDisposable on the application class. The code generator for the entry point creates the application without disposing it at the end. This might confuse developers.

The fix would be quite simple by just generating a using statement if the application class implements IDisposable.

A workaround is possible by creating a custom entry point.

API Proposal

Generated output:

/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "8.0.5.0")]
public static void Main() {
	SplashScreen splashScreen = new SplashScreen("splash.png");
	splashScreen.Show(true);
	using (App app = new App())
	{
		app.InitializeComponent();
		app.Run();
	}
}

API Usage

public partial class App : Application, IDisposable
{
    public void Dispose()
    {
        // Dispose resources
    }
}

Alternative Designs

No response

Risks

Application classes already implementing IDisposable will get an additional call to Dispose. This should be a small issue as Dispose should be safe to call multiple times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API suggestionEarly 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