Nello script #95 abbiamo visto com'è possibile eseguire il download di file remoti in background, grazie a BackgroundDownloader, presente nel namespace Windows.Networking.BackgroundTransfer.
I download in background escono dal ciclo di vita dell'app, quindi dobbiamo monitorarli grazie al metodo GetCurrentDownloadsAsync di BackgroundDownloader.
Il metodo restituisce una collezione di DownloadOperation di cui possiamo recuperare l'IAsyncOperationWithProgress con AttachAsync e staccare il suo task con il noto overload AsTask.
IReadOnlyList<DownloadOperation> downloads = await BackgroundDownloader.GetCurrentDownloadsAsync(); foreach (var download in downloads) { var progress = new Progress<DownloadOperation>(OnProgress); try { IAsyncOperationWithProgress<DownloadOperation, DownloadOperation> downloadOperation = download.AttachAsync(); await downloadOperation.AsTask(progress); } catch (Exception ex) { status.Text = ex.Message; } }
Al metodo AsTask possiamo passare come parametro un oggetto di tipo Progress
private void OnProgress(DownloadOperation operation) { var uri = operation.RequestedUri; status.Text = operation.Progress.Status.ToString(); progress.Text = (operation.Progress.BytesReceived * 100 / operation.Progress.TotalBytesToReceive).ToString("0"); }
Commenti
Per inserire un commento, devi avere un account.
Fai il login e torna a questa pagina, oppure registrati alla nostra community.
Approfondimenti
Creazione di plugin per Tailwind CSS: espandere le Funzionalità del Framework
Potenziare Azure AI Search con la ricerca vettoriale
Utilizzare politiche di resiliency con Azure Container App
Esportare ed analizzare le issue di GitHub con la CLI e GraphQL
Gestire domini wildcard in Azure Container Apps
Disabilitare automaticamente un workflow di GitHub
Hosting di componenti WebAssembly in un'applicazione Blazor static
Utilizzare Model as a Service su Microsoft Azure
Ottimizzazione dei block template in Angular 17
Reactive form tipizzati con FormBuilder in Angular
Generare velocemente pagine CRUD in Blazor con QuickGrid
Persistere la ChatHistory di Semantic Kernel in ASP.NET Core Web API per GPT