---
title: Console application lifecycle
summary: Executes the Initialize()/Execute()/Finish() lifecycle for the resolved application instance.
status: draft
owner: <owner>
last_reviewed: 2026-09-15
library: SAIF.Framework.Windows
reference_type: package
operation_id: OP-WIN-002
trigger: Called by the program runner entry point after the Autofac container resolves the application.
---

# Console application lifecycle

`ConsoleBase<TArgContainer>.Run()` executes the standardized application lifecycle — `Initialize()`, `Execute()`, `Finish()` — for the concrete application instance resolved by the [program runner entry point](program-runner-run.md).

| Field | Value |
| --- | --- |
| **Operation ID** | OP-WIN-002 |
| **Trigger** | Called by [program runner entry point](program-runner-run.md) step 8, on the container-resolved application instance. |
| **Inputs** | State established by the constructor (`Arguments`, `StartTime`, injected `IConsoleLogService`). |
| **Outputs** | `Environment.ExitCode` reset to `0` at the start of this method; console header/footer log lines. |
| **Primary components** | `ConsoleBase<TArgContainer>.Run()`, `Dispose()` |
| **Entry point** | `Console\SAIF.Console\Console\ConsoleBase.cs` |

## Steps

1. `Initialize()` — abstract, app-specific one-time setup.
2. `Environment.ExitCode = 0` — set unconditionally after `Initialize()` returns ([BR-WIN-006](../business-rules.md#br-win-006-successful-initialize-resets-exit-code): a successful `Initialize()` always resets any pre-existing non-zero exit code before `Execute()` runs).
3. `Execute()` — abstract, app-specific main logic.
4. `Finish()` — abstract, app-specific cleanup.
5. Separately, on `Dispose()` (not part of `Run()`): `WriteFooter()` then `logService.Dispose()` — writes the "ended at"/"ran for N seconds" lines and disposes all registered `LogWriter`s.

Note: `WriteHeader()` runs earlier, in the constructor (before `Run()` is ever called), writing the "began at" line.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([Run invoked]) --> Init[Initialize]
	Init --> Reset[Environment.ExitCode = 0]
	Reset --> Execute[Execute]
	Execute --> Finish[Finish]
	Finish --> EndOk([Run returns])
	EndOk -.->|On Dispose| Footer[WriteFooter + logService.Dispose]
```

## Failure and edge-case behavior
- `Environment.ExitCode` is always reset to `0` after `Initialize()` succeeds, discarding any prior non-zero value set before this call — this happens regardless of what `Execute()`/`Finish()` subsequently do.
- `WriteFooter()` and log-service disposal run only on `Dispose()`, not as part of `Run()` — a consuming application that does not dispose the resolved `ConsoleBase` instance never writes the footer or flushes/disposes log writers.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

## Sources
- `Console\SAIF.Console\Console\ConsoleBase.cs`
