DesktopEventLoop class

Namespace Runic.Desktop in Runic.Desktop 0.23.0

public static class DesktopEventLoop

Runs asynchronous application work while the process main thread services native events.

Remarks

DesktopEventLoop.Run picks the loop for the platform and the configured window host: the GTK 4 runner when the host uses it, the macOS AppKit loop on macOS, and a plain wait elsewhere.

Methods

InvokeOnMainThreadAsync

public static ValueTask InvokeOnMainThreadAsync(Action action, CancellationToken cancellationToken = default)

Runs action on the macOS process main thread, where AppKit must be called.

Parameters

action

The work to run on the main thread. An exception it throws faults the returned task.

cancellationToken

Cancels the work before it starts.

Exceptions

ArgumentNullException

action is null.

PlatformNotSupportedException

Not on macOS.

InvalidOperationException

Called from another thread while no Desktop event loop runs.

OperationCanceledException

cancellationToken was cancelled.

Remarks

For native interop without a window, such as AppKit calls of an application behind a status icon; window-bound work uses DesktopWindow.InvokeNativeAsync. The action runs through the main dispatch queue, in order with Runic's own main-thread work, also while a menu is open, a window is resized live or a modal panel runs. Called on the main thread, it runs at once. From another thread it needs DesktopEventLoop.Run or DesktopEventLoop.Run to run, with or without a window; work still queued when the loop ends fails with InvalidOperationException. Keep the action short: the main thread serves windows and menus.

Cancelling cancellationToken before the action starts keeps it from running and cancels the task; an action that started runs to completion.

As with Task.Run, a lambda whose body is an expression with a value, such as () => x = 1, calls DesktopEventLoop.InvokeOnMainThreadAsync; use a statement body, () => { x = 1; }, for an action.

InvokeOnMainThreadAsync

public static ValueTask<T> InvokeOnMainThreadAsync<T>(Func<T> func, CancellationToken cancellationToken = default)

Runs func on the macOS process main thread and returns its result.

Type parameters

T

The type of the result.

Parameters

func

The work to run on the main thread. An exception it throws faults the returned task.

cancellationToken

Cancels the work before it starts.

Returns

The value func returned.

Exceptions

ArgumentNullException

func is null.

PlatformNotSupportedException

Not on macOS.

InvalidOperationException

Called from another thread while no Desktop event loop runs.

OperationCanceledException

cancellationToken was cancelled.

Remarks

As DesktopEventLoop.InvokeOnMainThreadAsync.

Run

public static int Run(DesktopHostOptions options, Func<DesktopHost, Task<int>> application)

Starts a Desktop host and runs application on the event loop its window host needs.

Parameters

options

The host options, for example new DesktopHostOptions().WithGtk4().

application

The application work. Its result becomes the return value.

Returns

The value returned by application.

Exceptions

InvalidOperationException

A Desktop event loop is already running in this process.

Remarks

Call this directly from Main, before any await, and return its result as the exit code. The options are validated and the host is built before a loop starts, so an options error never uses up a native toolkit's single per-process lifetime. The loop is picked from the platform and DesktopHostOptions.WindowHostFactory:

An event-loop factory that is not supported, or whose IDesktopEventLoopWindowHostFactory.PrepareEventLoop reports that it cannot run (for example because no display is available), is not started. The reasons are reported as warnings to DesktopHostOptions.DiagnosticSink and the Desktop logger, and the host runs on the plain loop so a browser fallback can open. The host is disposed before the loop ends.

Ctrl+C, SIGTERM and a closed Windows console stop the application in the same order as closing its last window: DesktopHost.ShutdownRequested is cancelled, the open windows close so DesktopWindow.WaitForCloseAsync returns, the application releases its work and returns, and the host is disposed before the loop ends. An application without a window, such as one behind a status icon, sets DesktopHostOptions.HandlesShutdownWithoutWindow and ends its work when DesktopHost.ShutdownRequested is cancelled; without that option, a signal that arrives while the host has no surface keeps its default action and ends the process. A second Ctrl+C, a second or later after the first signal, ends the process immediately; copies forwarded by dotnet run or dotnet watch do not. Set DesktopHostOptions.HandleShutdownSignals to false to keep the default signal handling.

On macOS, Quit (Command and Q), the Dock's Quit and logging out take the same order after DesktopHostOptions.ConfirmQuitAsync and each window's close guard agreed, also with no window open; the call then returns the application's result. Runic sets the application delegate before the application starts; see MacOSDesktopOptions.ForwardingApplicationDelegate.

Run

public static void Run(Func<Task> application)

Runs application on the platform's main-thread loop, without a DesktopHost.

Exceptions

InvalidOperationException

A Desktop event loop is already running in this process.

Remarks

The low-level loop for a process that uses native platform services but starts no Desktop host, such as a tool that shows a status icon or calls AppKit APIs on macOS. Call it from the synchronous process entry point. On macOS it creates the shared NSApplication with Runic's application delegate before application starts, runs AppKit's own loop ([NSApp run]) on the main thread until application completes, and then runs the main-thread work it queued last, such as native release; other platforms join the task.

An application that opens windows uses DesktopEventLoop.Run instead: it validates the options, runs a window host's own loop such as GTK 4, handles Ctrl+C and SIGTERM, disposes the host and returns the exit code. This overload does none of that.