# Application and Window Lifetime

Nexamas UI applications start through `MASApplication`. A `MASApplication` owns application-level settings and creates one or more `MASApplicationWindow` instances attached to WinForms owner forms.

## Imports

```vbnet
Imports System.Windows.Forms
Imports Nexamas.UI.Application
```

## Explicit lifetime pattern

Use this pattern when the host form owns a MAS window and you want disposal to be obvious:

```vbnet
Public Class MainForm
    Inherits Form

    Private ReadOnly _application As MASApplication
    Private ReadOnly _window As MASApplicationWindow

    Public Sub New()
        InitializeComponent()

        _application = MASApplication.Create()
        _window = _application.CreateWindow(owner:=Me)

        _window.Controls.LayoutPage(
            Sub(page)
                page.FullWidth(_window.Controls.AddLabel("Hello Nexamas UI"), MASSize.FillWidth)
                page.Add(_window.Controls.AddButton("Continue"), MASSize.Default)
            End Sub)
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        _window.Dispose()
        _application.Dispose()
        MyBase.OnFormClosed(e)
    End Sub
End Class
```

## Runtime configuration

`MASApplication.ConfigureRuntime(...)` is process-wide. Call it before the first MAS window is created:

```vbnet
MASApplication.ConfigureRuntime(
    enableDiagnosticsDiskLogging:=True,
    throwOnSwallowedException:=False,
    throwOnLifecycleBreakingException:=False)
```

After a MAS window exists, runtime startup/diagnostics policy is intentionally frozen while live windows remain. This avoids one window silently changing the runtime behavior of another window.

## Window facades

A `MASApplicationWindow` exposes typed public facades. Use these before considering any lower-level host concepts:

| Facade | Use it for |
|---|---|
| `window.Controls` | Adding controls and retained layout |
| `window.Shell` | Standard application chrome such as TopBar/MenuBar |
| `window.Theme` | Selecting or refreshing the active theme for this window |
| `window.Sizing` | Applying size profiles and refreshing size-sensitive layout |
| `window.Geometry` | Asking MAS for layout-owned anchor bounds |
| `window.Input` | Public input-facing services |
| `window.Services` | Dialog and host services exposed through Application ownership |
| `window.Pages` | Same-window page registration/navigation |
| `window.Chrome` | Native window chrome behavior |

## Do not use as application start points

Do not start a consumer app from RootHost, render engines, layout engines, diagnostics channels, or Showcase helpers. The public start path is `MASApplication.Create()` then `CreateWindow(owner)` or the documented attachment convenience route.

## Related pages

- [Layout and composition](layout-and-composition.md)
- [Diagnostics and quality proof](diagnostics-and-quality-proof.md)
- [Application getting started](../developer/application-getting-started.md)
- [Application facade reference](../reference/application-facade-surface.md)

## Evidence

- `MASSystem/Application/Facade/MASApplication.vb`
- `MASSystem/Application/Facade/MASApplicationWindow.vb`
- `MASSystem/Application/Facade/MASApplicationDiagnostics.vb`
- `samples/MinimalConsumerApp/MainForm.vb`
