Repository navigation
4. 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.
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.
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();
}builder.Services.AddDirectNComponent(provider => new MainWindowComponent(
provider.GetRequiredService<ILogger<MainWindowComponent>>(),
provider.GetRequiredService<MainWindowHolder>()));The factory runs on the UI thread.
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);
}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.
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).
Scarlet.Extensions.Hosting.DirectN
Getting Started
Core Features
Extensions
Reference