Skip to content

4. Components 🧩

Artyom M. edited this page Nov 5, 2026 · 2 revisions

Components

A component is an object that lives on the DirectN UI thread: typically something that creates windows, or a tray icon. It replaces what a WPF application did in App.xaml and its App class.

Overview

public interface IDirectNComponent
{
    void InitializeComponent();
}
  • Components are created with constructor injection from the container, on the UI thread.
  • InitializeComponent() runs on the UI thread before the message loop starts, in registration order.
  • If a component implements IDisposable, it is disposed on the UI thread after the message loop has ended, in reverse order. The container never disposes it.
  • A component that throws from InitializeComponent() fails host startup (host.Run() throws), and the components created so far are disposed.

Registering a Component

builder.Services.AddDirectNComponent<MainWindowComponent>();

The component isn't registered as a service: the host keeps a factory and creates it on the UI thread. Its dependencies are resolved from the container:

public sealed class MainWindowComponent(ILogger<MainWindowComponent> logger, MainWindowHolder holder) : IDirectNComponent, IDisposable
{
    private Window? _window;

    public void InitializeComponent()
    {
        _window = new Window("My app");
        _window.ResizeClient(800, 600);
        _window.Center();
        _window.Show();
        holder.Window = _window;
        logger.LogInformation("Main window created");
    }

    public void Dispose() => _window?.Dispose();
}

Custom Factory

builder.Services.AddDirectNComponent(provider => new MainWindowComponent(
    provider.GetRequiredService<ILogger<MainWindowComponent>>(),
    provider.GetRequiredService<MainWindowHolder>()));

The factory runs on the UI thread.

Reaching the UI from Services

Components run on the UI thread; services usually don't. To let services reach a window, keep it in a singleton (as MainWindowHolder above) and switch to the UI thread before touching it, with the injectable DirectNDispatcher (namespace Scarlet.DirectN.Threading, Message Queue), JoinableTaskFactory.SwitchToMainThreadAsync() (Threading) or IDirectNThread.SynchronizationContext:

public sealed class StatusService(DirectNDispatcher dispatcher, MainWindowHolder holder)
{
    public void ShowStatus(string text) => dispatcher.Post(() => holder.Window!.Text = text);

    public Task<string> ReadTitleAsync() => dispatcher.InvokeAsync(() => holder.Window!.Text);
}

Order and Disposal

UI thread starts
β”œβ”€β”€ DirectNOptions.OnStartup
β”œβ”€β”€ ComponentA.InitializeComponent()
β”œβ”€β”€ ComponentB.InitializeComponent()
β”œβ”€β”€ message loop runs ... exits
β”œβ”€β”€ remaining windows are closed
β”œβ”€β”€ DirectNOptions.OnExit
β”œβ”€β”€ ComponentB.Dispose()
└── ComponentA.Dispose()

Every component is disposed even when another one throws from Dispose; each failure is logged as an error.

Why Not Register Components as Services?

The container disposes the disposable services it created, on whatever thread disposes the host, and only after the UI thread has ended. UI objects must be created and released on the UI thread, while the services they use still work, so the host creates components from a factory and disposes them itself (dotnet/runtime#36491).

Clone this wiki locally