This project demonstrates a high-performance, multi-threaded Blazor WebAssembly application using .NET 10. It enables native C# threading and the Task Parallel Library (TPL) within the browser environment.
The project is configured to use the WasmEnableThreads capability of the .NET WebAssembly runtime. This allows the instantiation of real background Web Workers that map directly to .NET Threads.
- Configuration: Enabled via
<WasmEnableThreads>true</WasmEnableThreads>inClient.csproj. - Worker Pool: Presets a pool of 4 workers using
<WasmWorkerPoolSize>4</WasmWorkerPoolSize>to reduce startup latency.
Browser security requirements for SharedArrayBuffer (required for multi-threading) necessitate specific cross-origin isolation headers. Since standard local development servers often don't provide these, we utilize a Service Worker interception strategy.
- Mechanism: The Service Worker (
service-worker.js) intercepts all fetch requests and appends:Cross-Origin-Embedder-Policy: require-corpCross-Origin-Opener-Policy: same-origin
- Bootstrapping:
index.htmlincludes a script that detects if the page iscrossOriginIsolated. If not, and a service worker controller is present, it forces a reload to apply the headers to the current session.
Multithreading in Blazor WASM can cause "phantom breakpoints" or startup hitches when the .NET Debug Proxy attempts to synchronize worker threads.
- Fix: The
inspectUrihas been removed fromlaunchSettings.jsonto prevent the debug proxy from attaching to workers. - Filtering: The Service Worker explicitly ignores traffic to
_framework/debugand_framework/blazor-hotreloadto ensure initialization messages are not modified.
The application includes several workloads designed to stress background threads while keeping the main UI thread at a consistent 144 FPS:
- CPU Threads: Intensive prime number calculation (
ThreadandTask.Run). - Memory Stress: Massive array allocations and concurrent
Array.Sortoperations. - Parallel TPL: Demonstrates
Parallel.Forwith configurable degrees of parallelism.
/Pages: Benchmark implementations and the UI thread FPS visualizer./Components:SmoothnessVisualizer.razormonitors the main thread responsiveness./wwwroot/service-worker.js: The header injection engine.Client.csproj: MSBuild properties for threading and stability.
- Navigate to
src/Client. - Execute
dotnet run. - Open the browser and ensure the Service Worker is active.
- Note: If headers are not applying, unregister the Service Worker in DevTools and perform a Hard Refresh (Ctrl+F5).
- .NET 10 SDK or higher.
- A modern browser with
SharedArrayBuffersupport (Chrome, Edge, Firefox).
