I have the following scenario:
- file download from a site;
- process records on file to store in a database.
The first take about 4 seconds and the second around a minute or a little more.
Followed the sample of ProgressBar to signal that records are being processed, however, the UI is not being updated.
The environment is Blazor with .NET 5.04 and the latest version of UI for Blazor
Below is the Razor component where the data is handled.
Any suggestions on how to update the UI and reflect the current status?
Thanks
@page "/importPeers"
@using DataLoader.Models
@using DataLoader.Services
@using System.Timers
@implements IDisposable
@inject LoadDataFIService service
<h1>CVM FI Download</h1>
<p>componente para realizar o download de informações da CVM e carga no banco de dados</p>
<hr />
<br />
<div class="row">
<div class="col-sm-8 col-md-8">
<div class="row">
<div class="col-sm-6 col-md-6">
<span>
Mês Referência:
<TelerikDatePicker @bind-Value="@selectedDate"
BottomView="@CalendarView.Year"
Format="MMM yyyy">
</TelerikDatePicker>
</span>
</div>
<div class="col-sm-3 col-md-3">
<TelerikButton OnClick="@OnProcessDataClick">Processar</TelerikButton>
</div>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-sm-8 col-md-8">
@processingAction
<br />
<TelerikProgressBar Indeterminate="@isIndeterminate" Value="@CurrentValue" Max="@MaxValue" />
</div>
</div>
@currentCount importados...
<hr />
@code {
public string processingAction { get; set; }
public bool isIndeterminate { get; set; } = false;
public int progressValue { get; set; } = 0;
DateTime selectedDate { get; set; } = DateTime.Now;
int currentCount = 0;
public double MaxValue { get; set; } = 100;
public double CurrentValue { get; set; } = 0;
public double StepValue { get; set; } = 5;
public Timer Clock { get; set; } = new Timer();
void OnProcessDataClick()
{
currentCount++;
ProcessData();
}
void ProcessData()
{
//signal progress bar for download
processingAction = "Downloading...";
MaxValue = 5 * 1000; // download should take no more than 4 seconds
StartProgress();
var result = service.DownloadFile(selectedDate);
// signal end of downloading
processingAction = "Downloaded!";
StopProgress();
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
if (result)
{
processingAction = "Processing records...";
MaxValue = 65 * 1000; // data processing should last about a minute or more
StartProgress();
//watch.Start();
currentCount = service.ReadCsvFile(selectedDate);
//watch.Stop();
processingAction = "Records processed!";
StopProgress();
}
// Console.WriteLine(watch.Elapsed);
}
public void Dispose()
{
StopProgress();
Clock?.Close();
}
public void StartProgress(int interval = 200)
{
if (Clock?.Enabled == false)
{
Clock.Interval = interval;
Clock.Elapsed -= OnClockElapsedEvent;
Clock.Elapsed += OnClockElapsedEvent;
Clock.AutoReset = true;
Clock.Start();
}
}
public void OnClockElapsedEvent(Object source, ElapsedEventArgs e)
{
if (CurrentValue < MaxValue)
{
UpdateProgress();
}
else
{
StopProgress();
}
}
public void UpdateProgress()
{
CurrentValue += StepValue;
InvokeAsync(StateHasChanged);
}
public void StopProgress()
{
Clock?.Stop();
InvokeAsync(StateHasChanged);
}
}