Skip to content

13. FAQ & Troubleshooting ❓

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

FAQ & Troubleshooting

Frequently Asked Questions

How do I migrate from Extensions.Hosting.Wpf?

See Migrating from Extensions.Hosting.Wpf. The hosting model is the same; the App class is replaced by options and components.

Should I use Host.CreateApplicationBuilder or Host.CreateDefaultBuilder?

Either. Every registration is an IServiceCollection extension (AddDirectN, AddDirectNLifetime, AddDirectNComponent, ...), so the same calls work on builder.Services and inside IHostBuilder.ConfigureServices, with the same behavior and ordering. Host.CreateApplicationBuilder is the newer, recommended API.

Do I need AddDirectNLifetime / UseDirectNLifetime?

No, it's optional: with any host lifetime, the host stops when the application exits, and the application exits when the host stops. DirectNLifetime replaces the console lifetime with one suited to a windowed application. See DirectN Lifetime.

Can I use the UI from a hosted service?

Yes. Use DirectNDispatcher (Post, or await InvokeAsync(...)), post to IDirectNThread.SynchronizationContext, or await JoinableTaskFactory.SwitchToMainThreadAsync(), at any time: work requested before the UI thread exists runs once it has started (the UI thread starts after every hosted service has started). Don't wait for it inside a hosted service's StartAsync (awaiting InvokeAsync or SwitchToMainThreadAsync() there, or JoinableTaskFactory.Run): the UI thread only starts after every StartAsync has returned, so host startup would hang. Do such work from BackgroundService.ExecuteAsync or IHostApplicationLifetime.ApplicationStarted. Only a synchronous Send is unavailable before then, see below.

When the host stops, the UI thread has already ended before any hosted service's StopAsync runs.

Does it work with Native AOT?

Yes, that is the point of it. Every package is IsAotCompatible and verifies that its references are too. CI publishes the samples, the scenario app and the library tests with PublishAot and runs them natively.

Can I host two DirectN applications?

No. DirectN allows one Application per thread and none after its process-wide exit, so there is one DirectN host per process.

Can I register multiple tray icons?

Yes. Each AddDirectNTrayIcon<T>() adds one; each TrayNotifyIcon gets its own icon in the notification area.

What happens when the user logs off or Windows shuts down?

Confirmed session ends and Restart Manager shutdown requests are handled: the application is cleaned up (OnExit, components disposed) and the host stopped, the same as a normal exit, within HostOptions.ShutdownTimeout. Once the application has handled WM_ENDSESSION, Windows may end the process, so this happens while handling it, not later through the message loop. Don't rely on more time than that: Windows (or Restart Manager) can still force termination.

Do AsyncLocal values flow to the UI thread?

Yes. Work runs in the ExecutionContext of whoever posted it, like with WPF's Dispatcher and WinForms' BeginInvoke, also when posted before the UI thread existed. See Message Queue.

Troubleshooting

My startup exception is thrown from host.Run()

That's by design: an exception from OnStartup or a component's InitializeComponent, or a DirectN error before the message loop runs, fails host startup instead of crashing the process. The exception is the original one; the components created so far were disposed.

"Send is only available while the DirectN UI thread runs"

IDirectNThread.SynchronizationContext.Send was called before the UI thread exists, typically from a hosted service's StartAsync. Blocking there would deadlock: the UI thread starts after StartAsync. Use Post, or await JoinableTaskFactory.SwitchToMainThreadAsync(), which complete once the UI thread runs.

"The DirectN UI thread's queue is closed or its thread has exited; the work did not run."

Send (or Post on the queue itself) was called while the application was shutting down or after it ended. The work didn't run. Posting through IDirectNThread.SynchronizationContext or SynchronizationContext.Current on the UI thread (what await does) never throws for this; the work is dropped.

"A TrayNotifyIcon can only be used on the thread that created it."

A TrayNotifyIcon member was used from another thread. Switch to the UI thread first (SwitchToMainThreadAsync, or post to IDirectNThread.SynchronizationContext). Dispose is the only member callable from any thread.

"A DirectNMessageQueue can only be closed by its own thread."

DirectNMessageQueue.Close() was called from another thread. Close it from the thread that owns it, after its message loop has ended. The host does this for its UI thread.

"0003: It's not possible to create applications any more." / "0006: There can be only one Application instance for a given thread."

DirectN errors: a second DirectN host, or a DirectN Application created by your own code, in a process that already has one. Use one host per process, and let the host create the Application.

"Work was posted to the DirectN UI thread before it started"

A warning, not an error: work was posted before the UI thread existed. It runs once the application has started.

"work item(s) posted to the DirectN UI thread never ran because the DirectN application was not started"

The host was disposed without its UI thread ever running (startup failed or was never attempted), so work posted for it was dropped.

The tray icon doesn't show

  • Visible must be true and Icon set; the icon only appears when both are.
  • TrayIconImage.FromApplicationIcon() needs <ApplicationIcon> in the project file.
  • With ShutdownMode.OnLastWindowClose and no window, the application exits right away; tray applications use OnExplicitShutdown.

The app exits when a secondary window closes, or doesn't exit when the main one closes

With ShutdownMode.OnLastWindowClose, DirectN exits when the last non-background window closes, and marks every window created after the first one as a background window. Set Window.IsBackground to change that per window, or use OnExplicitShutdown and exit with IDirectNThread.HandleApplicationExit().

Clone this wiki locally