Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload "DownloadFile" with an option for asynchronous progress #36

Closed
wants to merge 1 commit into from
Closed

Conversation

Jack-Edwards
Copy link
Contributor

@Jack-Edwards Jack-Edwards commented Apr 15, 2022

The existing "IProgress" options are not working well for me in Blazor WASM on .NET 6.0.

I want to update my UI every time the progress updates. Instead, none of the progress updates fire until the download completes. My progress bar shoots from 0% to 100% in an instant.

This Func<double, Task> option works much better. My usage:

await BlazorDownloadFileService.DownloadFile(FileName, DecryptedFile, downloadBufferSize, ContentType, UpdateDownloadProgressAsync);

protected async Task UpdateDownloadProgressAsync(double value)
{
    DownloadProgressPercent = value;
    await Task.Delay(5);  // Give the UI a moment to update
    StateHasChanged();  // May not be necessary
    Console.WriteLine(value);  // Debug
}

@Jack-Edwards
Copy link
Contributor Author

Similar to the user in #21 , I don't think I will actually be using this code in my application. Partitioned downloads are so much slower than all-at-once downloads. I wish that were not the case.

@arivera12
Copy link
Owner

Hi @Jack-Edwards

For some and mostly cases an entire download can't be just be downloaded at once for an example blazor server.

Since you have limits on the buffer size and if you exceeds blazor server max buffer size for signal r this will raise an error on the client side because the blazor server will disconnect for that connection.

In your case since you are using blazor wasm it could be possible to download all at once and that is the use of the parameter buffersize.

To download all at once you would need to set the buffersize to the lenght of the file.

I haven't tested NET6 as you can see here #32

But is pretty weird that the progress is not being reported correctly for you over blazor wasm and net6.0.

I need to take some time and test over net6 since the javascript code I have is backward compatible with older blazor versions and I want to keep it like that for now.

@arivera12
Copy link
Owner

arivera12 commented Apr 16, 2022

@Jack-Edwards

Does the IProgress reporting correctly to you at least?

Or are we not getting any value from it?

@Jack-Edwards
Copy link
Contributor Author

Jack-Edwards commented Apr 16, 2022

@Jack-Edwards

Does the IProgress reporting correctly to you at least?

Or are we not getting any value from it?

The IProgress does report correctly as far as the C# WASM runtime goes. But when I call StateHasChanged() or even Console.WriteLine() (which writes to the browser console), the synchronous IProgress report does not give the browser any time to react.

It is only after the partitioned download completes do I see my UI update or my console statements appear in the browser console.

Converting the synchronous IProgress to an asynchronous Task and awaiting a very small Task.Delay() during this progress report gives the browser enough time to process any updates.

@Jack-Edwards
Copy link
Contributor Author

I imagine this issue with synchronous IProgress reports in WASM could be resolved when Blazor WASM gets multi-thread support.

@arivera12
Copy link
Owner

arivera12 commented Apr 16, 2022

@Jack-Edwards

Makes sense and as far I remember once a time a go I run into a similar issue where an async api didn't report any synchronous operation until the task was fully completed so to overcome this we would need another task that needs to be awaited/completed to report any progress.

What you suggest here works properly for you case using Func<double, Task> ?

Please confirm to proceed with the changes over these days.

@Jack-Edwards
Copy link
Contributor Author

Yes, the sample code I provided in the PR description does work. The UI will update and my console statements will appear in the browser console every time the provided Task is awaited.

Take your time if you want to incorporate these changes. I am not blocked.

@arivera12
Copy link
Owner

arivera12 commented Apr 16, 2022

@Jack-Edwards

Give me a few labor days these week and I will keep you updated.

I am currently taking a break these days.

@arivera12
Copy link
Owner

@Jack-Edwards

Install-Package BlazorDownloadFile -Version 2.4.0.0

@arivera12 arivera12 closed this Apr 23, 2022
@Jack-Edwards
Copy link
Contributor Author

@arivera12 I believe progress?.Invoke(totalProgress); should be await progress?.Invoke(totalProgress); in the latest code.

@arivera12
Copy link
Owner

arivera12 commented Apr 24, 2022

@Jack-Edwards is totally true, you are right, I am rusty writing code, let me fix and create new package.

@arivera12
Copy link
Owner

@Jack-Edwards

Install-Package BlazorDownloadFile -Version 2.4.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants