How handle progress in BackgroundTransfer DownloadOperation C++/WinRT

359 views Asked by At

I try handle progress of downloading file via BackgroundTransfer::BackgroundDownloader in c++/WinRT Windows 10 UWP App.

I read the documentation 10 times yesterday: https://learn.microsoft.com/en-us/uwp/api/windows.foundation.iasyncoperationwithprogress-2.progress?view=winrt-19041#Windows_Foundation_IAsyncOperationWithProgress_2_Progress

but i cant implement this correct :(

this is my sample code (file downloading well, but if i put .Progress function i got error: Error C2280 'void *winrt::Windows::Foundation::IUnknown::operator new(size_t)': attempting to reference a deleted function NativeModuleSample

code:

BackgroundTransfer::BackgroundDownloader downloader{ BackgroundTransfer::BackgroundDownloader() };
BackgroundTransfer::DownloadOperation download = downloader.CreateDownload(uri, destinationFile);
auto progress = download.StartAsync();

progress.Progress(Windows::Foundation::AsyncOperationProgressHandler<BackgroundTransfer::DownloadOperation, BackgroundTransfer::BackgroundDownloadProgress>(
    [=](Windows::Foundation::IAsyncOperationWithProgress<BackgroundTransfer::DownloadOperation, BackgroundTransfer::DownloadOperation> const sender, BackgroundTransfer::BackgroundDownloadProgress args) {
        int progress_calc = (int)(100 * ((double)args.BytesReceived / (double)args.TotalBytesToReceive));    
    }
));
1

There are 1 answers

0
YanGu On BEST ANSWER

To handle progress of downloading file via BackgroundTransfer::BackroundDownloader, you can refer to the Scenario1_Download of the sample in GitHub.

Based on the sample about downloading, you can change your code progress.Progress(…) to this:

progress.Progress(Windows::Foundation::AsyncOperationProgressHandler<Windows::Networking::BackgroundTransfer::DownloadOperation, Windows::Networking::BackgroundTransfer::DownloadOperation>(
    [=](Windows::Foundation::IAsyncOperationWithProgress<Windows::Networking::BackgroundTransfer::DownloadOperation, Windows::Networking::BackgroundTransfer::DownloadOperation> const sender, 
        Windows::Networking::BackgroundTransfer::DownloadOperation args) {
               
            BackgroundDownloadProgress currentProgress = args.Progress();

            UINT64 percent = 0;
            if (currentProgress.TotalBytesToReceive > 0)
            {
                percent =(INT64)((double)currentProgress.BytesReceived * 100 / (double)currentProgress.TotalBytesToReceive);
            }
    }
));