i'm new to blazor and stumbled upon something strange. If i do an infinite while loop in the OnInitializedAsync override, it will keep running after navigating to another page.
Furthermore, if i navigate back and forth, it will run multiple of these infinite while loops. As if it's creating a new instance on every navigation to the page, but doesn't clean up the instance when navigating away from the page.
How is this possible? And what would be the recommended approach to do continuous api requests (for updating data), as long as the user has not switched page? Or any kind of continuous task for that matter.
Thanks! -Victor
You should make your Components disposable and cancel any running Tasks/unsubscribe from any Events etc. in the Dispose method.
When Blazor is rendering the page, it will create a new instance of your Component, and when you remove a Component from the "page" (this includes navigating to another Component/page), if the Component implements IDisposable then Dispose will be called.
How you implement a long running task is up to you - usually it would go into a Service rather than a UI Component though.