dotnet / dotnet/wpf

[API Proposal]: Dispose Application in Generated Entry Point

Open
#10,074 7 comments 0 reactions 0 assignees View on GitHub
API suggestion
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### 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:
```csharp
///
/// Application Entry Point.
///
[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

```csharp
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.