I've recently inherited a Blazor Webassembly application, however have minimal experience with dotnet or Blazor.
Some of the components use await Task.Run(StateHasChanged)
rather than await InvokeAsync(StateHasChanged)
and I'm wondering if this is intentional.
I ask as await Task.Run(StateHasChanged);
gives me the following exception when attempting to render the component using bUnit:
System.InvalidOperationException The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state.
Changing this to await InvokeAsync(StateHasChanged);
allows the component to be rendered in bUnit. However, as far as I can tell, the component functions identically when using the application for either await Task.Run(StateHasChanged)
or await InvokeAsync(StateHasChanged)
.
What is the difference between these two approaches to invoking StateHasChanged
?