Scarlet.Extensions.Hosting.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.Extensions.Hosting.DirectN.Threading --version 1.0.0
                    
NuGet\Install-Package Scarlet.Extensions.Hosting.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.Extensions.Hosting.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.Extensions.Hosting.DirectN.Threading" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Scarlet.Extensions.Hosting.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.Extensions.Hosting.DirectN.Threading --version 1.0.0
                    
#r "nuget: Scarlet.Extensions.Hosting.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.Extensions.Hosting.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.Extensions.Hosting.DirectN.Threading&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Scarlet.Extensions.Hosting.DirectN.Threading&version=1.0.0
                    
Install as a Cake Tool

Scarlet.Extensions.Hosting.DirectN.Threading

JoinableTaskFactory-based thread switching for Scarlet.Extensions.Hosting.DirectN.

builder.Services.AddDirectN();
builder.Services.AddThreadSwitching();

public sealed class WindowService(JoinableTaskFactory jtf)
{
    public async Task ShowAsync()
    {
        await jtf.SwitchToMainThreadAsync();   // now on the DirectN UI thread
        // ... create or update windows ...
        await TaskScheduler.Default;           // back to the thread pool
    }
}

AddThreadSwitching() registers a JoinableTaskContext whose main thread is the DirectN UI thread, and its JoinableTaskFactory. It works before the UI thread has started: SwitchToMainThreadAsync() then completes once it has. Don't wait for that inside a hosted service's StartAsync (awaiting the switch, or JoinableTaskFactory.Run): the UI thread only starts after every StartAsync has returned, so host startup would hang. Use BackgroundService.ExecuteAsync or IHostApplicationLifetime.ApplicationStarted instead.

Contents

A trimmed, Native AOT compatible extract of Microsoft.VisualStudio.Threading v17.12.19 (MIT, not affiliated with Microsoft). Type and member names are unchanged, so code moves over by replacing using Microsoft.VisualStudio.Threading; with using Scarlet.Extensions.Hosting.DirectN.Threading;.

Public types: JoinableTaskContext, JoinableTaskContextNode, JoinableTaskFactory, JoinableTask, JoinableTask<T>, JoinableTaskCollection, JoinableTaskCreationOptions, JoinableTaskContextException, AsyncLazy<T>, and AwaitExtensions (await TaskScheduler.Default, SwitchTo).

var settings = new AsyncLazy<Settings>(LoadSettingsAsync, jtf);
var value = await settings.GetValueAsync();

Differences from vs-threading

  • Not included: AsyncReaderWriterLock, AsyncSemaphore, ReentrantSemaphore, AsyncManualResetEvent (internal), the other async primitives, WPF Dispatcher support, and the hang report (GetHangReport; hang detection JoinableTaskContext.OnHangDetected is kept). See UPSTREAM.md for the full list.
  • AwaitExtensions has the same name and members as upstream's: in a project that also references Microsoft.VisualStudio.Threading, await TaskScheduler.Default is ambiguous when both namespaces are imported. Import only one of them in such files.
  • The VSTHRD analyzers (Microsoft.VisualStudio.Threading.Analyzers) recognize the Microsoft.VisualStudio.Threading namespace only, so they don't analyze these types.
  • JoinableTaskFactory.Run on the UI thread doesn't pump window messages while it waits (as upstream): work that needs the UI thread still runs through the factory's own queue, so it doesn't deadlock, but windows don't repaint until it returns.
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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 103 11/5/2026
1.0.0 92 11/3/2026