I am trying to perform two tedious Javascript tasks in parallel with Blazor WebAssembly in .NET 6. My current code looks like this:
task1 = Task.Run(async () =>
{
try
{
await JSInProcessRuntime.InvokeAsync<int>("BuildTablesOne", new object[] {data1});
}
catch (Exception exc)
{
await JSInProcessRuntime.InvokeAsync<object>("console.log", exc.Message);
}
});
task2 = Task.Run(async () =>
{
try
{
await JSInProcessRuntime.InvokeAsync<int>("BuildTablesTwo", new object[] {data2});
}
catch (Exception exc)
{
await JSInProcessRuntime.InvokeAsync<object>("console.log", exc.Message);
}
});
await Task.WhenAll(task1, task2);
These tasks seemingly don't run in parallel as I'd like. I estimate that each of the tasks takes around 2 seconds to finish separately. Totaling 4 seconds of execution time running after each other. In my ideal scenario I would be able to run these both in parallel for a total execution time of 2 seconds. Now I learned that Javascript does not have support for Multithreading but then I also came across WebWorkers and similar concepts and was thinking that maybe Blazor WASM has mechanisms to hook into those new concepts and tools somehow.
Is the above what I am trying to do even achievable, and if yes what can I change in the code to make this fully run in parallel?