1

Sorry for the very rudimentary question.

I'm writing a simple management app for production equipment on Blazor Server. I want to view that data on the Blazor page as the number of completed pieces is updated from the production equipment to the database every second.

Specifically, I want to refresh the page once the database is refreshed, or every few seconds (for example, every 10 seconds).

However, I have no idea how to make either of these. How should this be considered and implemented?

I'm sorry for the very abstract question, but I would like some advice. Thank you.

Quango
  • 12,338
  • 6
  • 48
  • 83
BTOM
  • 71
  • 7
  • [How to refresh web page after database update in ASP.NET CORE with Blazor](https://stackoverflow.com/questions/60075632/how-to-refresh-web-page-after-database-update-in-asp-net-core-with-blazor) Could this be helpful? – The2Step Sep 21 '21 at 13:45
  • Thank you very much. This is very good content. This content appears to be reacquiring the data with DbService.GetAllData () after an operation from the UI (eg Add). What I'm trying to do this time is that there is no particular operation from the UI, I want to refer to the database that is constantly being updated from a location other than the application and display the value. How should I reacquire the data if the operation from the UI cannot be used as the starting point? – BTOM Sep 21 '21 at 14:23
  • FYI, it is very difficult to provide a precise answer to your original question as you have not given any example code for your particular problem. But, after the database is updated, the `StateHasChanged` method can be called to refresh the page/UI. When called, this will re-render the component or the page based on the new changes from the database. Though this [link](https://stackoverflow.com/questions/55775060/blazor-component-refresh-parent-when-model-is-updated-from-child-component) isn't exactly what you're looking for, the accepted answer does show an example of using `StateHasChanged` – The2Step Sep 21 '21 at 14:45
  • It would be very useful to know what you have already tried, and what exact problem you are running into. Please consider reading [example] if you have not already. – The2Step Sep 21 '21 at 14:53
  • You need [a Timer](https://stackoverflow.com/a/69093342/60761) – H H Sep 21 '21 at 15:22
  • thank you for your answer. Sorry for the late reply. Also, I'm sorry to ask you a question without the sample code. In this article, I found that Timer and StateHasChanged () are very important points. Create and test a very simple sample as soon as possible. – BTOM Sep 22 '21 at 14:57

1 Answers1

1

So unless there is an event published from your database that your app can listen to, then you will need to query the database on a set interval.

This solution would fit your situation as well: Blazor Timer call async API task to update UI

Essentially you will set a timer. Here is Microsoft's general documentation: https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=net-5.0

What this looks like practically is that when the Component Initializes, you will put your code.

I personally had the following in the @code section:

private System.Timers.Timer DelayTimer;

async void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    await CheckForNotifications();
}

async Task CheckForNotifications()
{
    // Your Code goes here.
    StateHasChanged();
}

protected override async Task OnInitializedAsync()
{
    _log.Information("Component is Initialized.");
    await CheckForNotifications();

    DelayTimer = new System.Timers.Timer((double)(60 * 1000 * 3)); // 60 seconds * 3 is 3 minutes.
    DelayTimer.Elapsed += timer_Elapsed;
    DelayTimer.AutoReset = true;
    DelayTimer.Start();
    _log.Debug("Timer Started.");

}
Kevin
  • 26
  • 5
  • You really need to Dispose() the Timer. And to call StateHasChanged() somewhere. – H H Sep 21 '21 at 18:22
  • I'm really sorry for the late reply. Thank you for the very polite explanation. The reference links are useful to me. Let me check what you taught me. Execute the method with the specified feeling using Timer. In this Timer, executing StateHasChanged () re-renders the page and calls OnInitialized (). Therefore, by describing the process of acquiring the number of completed data from the database, which is what I want to do, in OnInitialized (), it should be possible to acquire data at regular intervals. Is this understanding correct? – BTOM Sep 22 '21 at 14:54
  • Yeah, I copied the code from one of my projects. In the "Your code goes here", I did have the "StateHasChanged" method. I added it into the code above. In terms of the Dispose, it does not appear to be required per Microsoft's documentation when you are resetting the timer: https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.autoreset?view=net-5.0 and https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.enabled?view=net-5.0 – Kevin Sep 22 '21 at 19:25
  • @BTOM StateHasChanged does not cause it to be re-initialized, but re-rendered (https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-5.0#state-changes-statehaschanged-1) . So your Timer can be setup in the OnInitialized Method. On your time_Elapsed method, you can call a method for your data. My "CheckForNotifications" method is the "container" for that. – Kevin Sep 22 '21 at 19:29
  • Sorry for the late reply. I tried to imitate the link as it is and see if the time can be displayed. [Blazor Timer call async API task to update UI](https://stackoverflow.com/questions/63060065/blazor-timer-call-async-api-task-to-update-ui) In the I got this code: part of the above link I get the following error in the StateHasChanged (); part. System.ObjectDisposedException:'Cannot process pending renders after the renderer has been disposed. ObjectDisposed_ObjectName_Name' Searching for a similar example does not tell me what is causing this. What's wrong with this? – BTOM Sep 30 '21 at 13:27
  • @Kevin Sorry for the late reply. I tried to imitate the link as it is and see if the time can be displayed. Blazor Timer call async API task to update UI In the I got this code: part of the above link I get the following error in the StateHasChanged (); part. System.ObjectDisposedException:'Cannot process pending renders after the renderer has been disposed. ObjectDisposed_ObjectName_Name' Searching for a similar example does not tell me what is causing this. What's wrong with this? – BTOM Sep 30 '21 at 13:38