Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor - OnAfterRenderAsync called again even previous is not finished

$
0
0

I run into problem with multiple times called OnAfterRenderAsync(bool firstRender) even when a previous run of the same method in the same component is not yet finished.

protected override async Task OnAfterRenderAsync(bool firstRender){    if (firstRender)    {        _jsModule = await JsRuntime.LoadJSModule(this);        await _jsModule!.InvokeVoidAsync("doLogic");    }    await _jsModule!.InvokeVoidAsync("doSomethingElse");}

What the problem is:

  1. On OnAfterRenderAsync runs first time with parameter firstRender = true
  2. It goes inside first condition
  3. I run StateHasChanged in parent component
  4. On AfterRenderAsync is called second time with parameter firstRender = false, even first run is still running and it's inside first condition
  5. Second run skipped first condition and goes directly to logic after, but _jsModule is still null here. So it fails

How this situation should be handled correctly? I tried use Semaphore, and then it works fine. But I think that this situation should be handled somehow by Blazor itself?

I found this problem with one component till now, because I run StateHasChanged in very short period of time.

I can imagine that it can happens also in other components and should be solved generally. Do you have some ideas or do I miss something?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>