Scarlet.Extensions.Hosting.DirectN.Threading
1.0.0
Prefix Reserved
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
<PackageReference Include="Scarlet.Extensions.Hosting.DirectN.Threading" Version="1.0.0" />
<PackageVersion Include="Scarlet.Extensions.Hosting.DirectN.Threading" Version="1.0.0" />
<PackageReference Include="Scarlet.Extensions.Hosting.DirectN.Threading" />
paket add Scarlet.Extensions.Hosting.DirectN.Threading --version 1.0.0
#r "nuget: Scarlet.Extensions.Hosting.DirectN.Threading, 1.0.0"
#:package Scarlet.Extensions.Hosting.DirectN.Threading@1.0.0
#addin nuget:?package=Scarlet.Extensions.Hosting.DirectN.Threading&version=1.0.0
#tool nuget:?package=Scarlet.Extensions.Hosting.DirectN.Threading&version=1.0.0
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, WPFDispatchersupport, and the hang report (GetHangReport; hang detectionJoinableTaskContext.OnHangDetectedis kept). See UPSTREAM.md for the full list. AwaitExtensionshas the same name and members as upstream's: in a project that also referencesMicrosoft.VisualStudio.Threading,await TaskScheduler.Defaultis ambiguous when both namespaces are imported. Import only one of them in such files.- The VSTHRD analyzers (
Microsoft.VisualStudio.Threading.Analyzers) recognize theMicrosoft.VisualStudio.Threadingnamespace only, so they don't analyze these types. JoinableTaskFactory.Runon 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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- Scarlet.Extensions.Hosting.DirectN (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.