I have an asynchronous method which is returning the entered user value of an input form. As long as the user didn't submit the input, the async method Task<String> Read() should be waiting. When the user submits the input form the method Task Execute(EditContext context) gets triggered. Therefore I used the TaskCompletionSource to block the Read method as long the form wasn't submit (which works for the wpf app, I did).
public async Task<String> Read()
{
StringReadTaskCompletionSource = new TaskCompletionSource<string>();
return await StringReadTaskCompletionSource.Task;
}
protected Task Execute(EditContext context)
{
//...
StringReadTaskCompletionSource
.SetResult((context?.Model as ConsoleInput as ConsoleInput).Text);
}
But with the above code I get:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot wait on monitors on this runtime. System.Threading.SynchronizationLockException: Cannot wait on monitors on this runtime. at (wrapper managed-to-native) System.Threading.Monitor.Monitor_wait(object,int) at System.Threading.Monitor.ObjWait (System.Boolean exitContext, System.Int32 millisecondsTimeout, System.Object obj) <0x2e64fc8 + 0x00046> in :0 at System.Threading.Monitor.Wait (System.Object obj, System.Int32 millisecondsTimeout, System.Boolean exitContext) <0x2e64ce8 + 0x00022> in :0 at System.Threading.Monitor.Wait (System.Object obj, System.Int32 millisecondsTimeout)
It looks like to be the result of the limitations of razor-wasm, regarding tasks and threads. I tried the workarounds from here: https://github.com/dotnet/aspnetcore/issues/14253#issuecomment-534118256
by using Task.Yield but was unsuccessful. Any idea how to workaround this issue?
[Edit:] I think the main conclusion for me is, that it's not possible with razor-wasm (because of the one thread limitation) to run a synchronous method (Console.ReadLine()), and waiting for a user input, without blocking the whole app. It looks like there is no workaround for this. The only way is to replace all this synchronously calls with a new async call like Console.ReadLineAsync().
Read? - Evk