Manages WPF Application lifecycle including Startup, Exit, SessionEnding events and single instance detection. Use when handling app initialization, shutdown, or preventing multiple instances.
View on GitHubchristian289/dotnet-with-claudecode
wpf-dev-pack
wpf-dev-pack/skills/managing-wpf-application-lifecycle/SKILL.md
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/managing-wpf-application-lifecycle/SKILL.md -a claude-code --skill managing-wpf-application-lifecycleInstallation paths:
.claude/skills/managing-wpf-application-lifecycle/# WPF Application Lifecycle Patterns
Managing application startup, shutdown, and runtime behavior.
**Advanced Patterns:** See [ADVANCED.md](ADVANCED.md) for single instance, settings, and activation handling.
## 1. Application Lifecycle Overview
```
Application Start
│
├─ App Constructor
├─ App.OnStartup() / Startup event
├─ MainWindow Constructor
├─ MainWindow.Loaded event
│
▼
Running State
│
├─ Activated / Deactivated events
├─ SessionEnding event (logoff/shutdown)
│
▼
Shutdown Initiated
│
├─ Window.Closing event (can cancel)
├─ Window.Closed event
├─ App.OnExit() / Exit event
│
▼
Application End
```
---
## 2. Startup Handling
### 2.1 App.xaml Startup
```xml
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="Application_Startup"
Exit="Application_Exit"
SessionEnding="Application_SessionEnding">
<Application.Resources>
<!-- Resources -->
</Application.Resources>
</Application>
```
### 2.2 Override OnStartup
```csharp
namespace MyApp;
using System.Windows;
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Process command line arguments
ProcessCommandLineArgs(e.Args);
// Initialize services
InitializeServices();
// Create and show main window manually (if StartupUri not set)
var mainWindow = new MainWindow();
mainWindow.Show();
}
private void ProcessCommandLineArgs(string[] args)
{
foreach (var arg in args)
{
if (arg.Equals("--debug", StringComparison.OrdinalIgnoreCase))
{
// Enable debug mode
}
else if (arg.StartsWith("-