Scarlet.DirectN.Threading 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Scarlet.DirectN.Threading --version 1.0.0
                    
NuGet\Install-Package Scarlet.DirectN.Threading -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Scarlet.DirectN.Threading" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Scarlet.DirectN.Threading" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Scarlet.DirectN.Threading" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Scarlet.DirectN.Threading --version 1.0.0
                    
#r "nuget: Scarlet.DirectN.Threading, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Scarlet.DirectN.Threading@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Scarlet.DirectN.Threading&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Scarlet.DirectN.Threading&version=1.0.0
                    
Install as a Cake Tool

Scarlet.DirectN.Threading

The UI-thread work queue and SynchronizationContext for DirectN (Win32) apps, Native AOT compatible. Used by Scarlet.Extensions.Hosting.DirectN. There is at most one queue per UI thread: one ordering, one exception path and one SynchronizationContext for everything on it.

// On the UI thread (it must run a message loop, e.g. DirectN's Application.Run()):
var queue = DirectNMessageQueue.GetOrCreateForCurrentThread();
SynchronizationContext.SetSynchronizationContext(queue.SynchronizationContext);
queue.UnhandledException += (_, e) => { Log(e.Exception); e.Handled = true; };

// From any thread:
queue.Post(_ => window.Text = "Updated", null);
  • Work is kept in memory and a single wake-up message is posted to a message-only window of the UI thread. Window messages are dispatched by every message loop, including modal ones (menus, message boxes, task dialogs, window move/resize), so queued work never stalls while one of those is open. Thread messages (PostThreadMessage) would be lost there.
  • At most 256 items, and no new item after about 15 ms (a frame), run per wake-up; a single long item can't be interrupted. When more are queued and input, paint or timer messages are pending, the queue continues from a timer (retrieved after all of them, as WPF does for background work), so a flood of posts can't starve them.
  • Like WPF's Dispatcher and WinForms' BeginInvoke, each item runs in the ExecutionContext captured from its caller (AsyncLocal values, culture), and what it changes there doesn't leak into later items. Work posted with flow suppressed runs in the UI thread's own baseline context.
  • DirectNSynchronizationContext.Send runs inline on the UI thread and otherwise blocks until the UI thread ran the work.
  • Exceptions from queued work raise UnhandledException; unless Handled is set they are rethrown on the thread pool and terminate the process, like WinForms and WPF.
  • TryPost returns false (and Post throws InvalidOperationException) only when the work can never run: the queue is closed or its thread has exited (IsClosed). A full thread message queue is retried, not reported.
  • DirectNSynchronizationContext.Post never throws: once the queue is closed the work is dropped, like WPF's and WinForms' contexts after shutdown. It is the context await resumes on, and the runtime turns an exception thrown there into one that terminates the process. DroppedCount counts the work dropped by closing.
  • A cross-thread Send returns once the work ran, or throws InvalidOperationException because the queue closed or its thread exited first, also when the thread exits without Close. Like any Send, it waits as long as the UI thread is alive but doesn't process messages (blocked, or busy in a long callback).
  • The queue has no capacity limit: don't flood the UI thread with unbounded work.
  • Call Close() on the owning thread when its message loop has ended for good (the host does this). A queue whose thread exits without it is still released by the garbage collector.
Member
DirectNMessageQueue.GetOrCreateForCurrentThread() The queue of the calling thread, created on first use.
DirectNMessageQueue.Current The queue of the calling thread, or null.
Post / TryPost Queue work for the UI thread.
Close() Owner thread only: drops queued work, rejects later posts, releases the window and native handle.
CheckAccess(), Thread, ManagedThreadId The owning thread.
SynchronizationContext The DirectNSynchronizationContext posting to this queue.
UnhandledException Exceptions from queued work.
RethrowAsUnhandled(exception) Surfaces an exception as unhandled without crossing a native callback boundary.

See the wiki for details.

Product Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Scarlet.DirectN.Threading:

Package Downloads
Scarlet.AspNetCore.Components.WebView.Win32Aot

Build native Win32 apps with Blazor and WebView2, NativeAOT and trimming friendly. Built on WebView2Aot (no WinForms/WPF, no built-in COM).

Scarlet.Extensions.Hosting.DirectN

Microsoft.Extensions.Hosting (Generic Host) integration for DirectN (Win32/Direct2D) desktop apps. Runs the DirectN UI thread inside the host, Native AOT compatible.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 169 11/5/2026
1.0.0 164 11/3/2026