Search code examples
c#blazorwebassembly

Blazor WASM Tasks


I have just seen that Blazor WASM does not currently support multiple threads. So if you do a Task.Run() it will actually run on the main thread and block the UI.

So if I wanted a Blazor WASM application that did some expensive processing. For example

a) A computationally expensive numerical computation

await Task.Run(() => SlowComputation())

or

b) Performing a slow query in a SQLite DB

would I essentially just have to accept that the UI will be blocked?

I'm currently in the process of converting a winforms app to Blazor wasm and we use Task.Run() quite frequently.


Solution

  • Any processor intensive operation such as a heavy duty computation will block the thread: there's only one. Consider handing off to an API.

    Any I/0 activity will not: there's no actual thread being used to wait on say the network subsystem returning the requested data. So calling Task.Run() in such circumstances is pointless. If you want an explanation see https://blog.stephencleary.com/2013/11/there-is-no-thread.html.

    You can't call a SQLite DB from the browser, that's got to go through an API.