c# - Downloading files not affecting UI -
we downloading files using httpclient
because our backend requires certificate.
i have control - filerow
which ui element code-behind
methods file downloading, one:
if (fileisdownloaded == false) { await coreapplication.mainview.corewindow.dispatcher.runasync(windows.ui.core.coredispatcherpriority.low, () => { datamanager.instance.downloadfile(this); }); } if (thumbnailisdownloaded == false) { await coreapplication.mainview.corewindow.dispatcher.runasync(windows.ui.core.coredispatcherpriority.low, () => { datamanager.instance.downloadthumbnail(this); }); }
downloading single item fine when click download ( 50 items ) whole ui starts freeze.
as can see, have tried give requests low priority - still same result.
answers common questions:
1) yes files should downloadable @ 1 time, not 1 after another.
2) datamanager.instance.downloadthumbnail(this)
- give refference current control report progress in progress bar.
any suggestions?
edit:
downloading looks this:
public async void downloadfile(filerow filerow) { //lot of checking if file exist, if version same string localfilename = await downloadmanager.downloadfile(filerow.myfile.file.id, filerow.myfile.file.version, filerow.myfile.file.filename,filerow); // next using filename string }
and download:
public static async task<string> downloadfileofcustomerassetrow(int? id, int? version, string filename, filerow filerow) { try { httpclienthandler ahandler = new httpclienthandler(); ahandler.clientcertificateoptions = clientcertificateoption.automatic; httpclient aclient = new httpclient(ahandler); customerassetrow.currentfiledownload = aclient; aclient.defaultrequestheaders.expectcontinue = false; httpresponsemessage response = await aclient.getasync(webservices.backendstarturl + "getfiledata?id=" + id + "&version=" + version, httpcompletionoption.responseheadersread); var file = await applicationdata.current.localfolder.createfileasync(filename, windows.storage.creationcollisionoption.generateuniquename); filerow.filename = file.name; using (var fs = await file.openasync(windows.storage.fileaccessmode.readwrite)) { stream stream = await response.content.readasstreamasync(); iinputstream inputstream = stream.asinputstream(); ulong totalbytesread = 0; while (true) { ibuffer buffer = new windows.storage.streams.buffer(1024); buffer = await inputstream.readasync( buffer, buffer.capacity, inputstreamoptions.none); if (buffer.length == 0) { break; } totalbytesread += buffer.length; filerow.progress.value = filerow.progress.value + 1024; await fs.writeasync(buffer); } inputstream.dispose(); } filerow.progress.visibility = visibility.collapsed; return file.name; } catch (exception e) { errorreporter.reporterror("error in downloadmanager.cs in function downloadfile.", e); return ""; } }
it's possible async
method continuations interrupting ui thread much. try creating stronger separation between background logic (downloadfileofcustomerassetrow
) , ui (filerow
) introducing iprogress<t>
reporter. ensure every await
in background logic has configureawait(false)
on it.
public static async task<string> downloadfileofcustomerassetrow(int? id, int? version, string filename, iprogress<int> progress) { httpclienthandler ahandler = new httpclienthandler(); ahandler.clientcertificateoptions = clientcertificateoption.automatic; httpclient aclient = new httpclient(ahandler); customerassetrow.currentfiledownload = aclient; aclient.defaultrequestheaders.expectcontinue = false; httpresponsemessage response = await aclient.getasync(webservices.backendstarturl + "getfiledata?id=" + id + "&version=" + version, httpcompletionoption.responseheadersread).configureawait(false); var file = await applicationdata.current.localfolder.createfileasync(filename, windows.storage.creationcollisionoption.generateuniquename).configureawait(false); filerow.filename = file.name; using (var fs = await file.openasync(windows.storage.fileaccessmode.readwrite).configureawait(false)) { stream stream = await response.content.readasstreamasync().configureawait(false); iinputstream inputstream = stream.asinputstream(); ulong totalbytesread = 0; while (true) { ibuffer buffer = new windows.storage.streams.buffer(1024); buffer = await inputstream.readasync( buffer, buffer.capacity, inputstreamoptions.none).configureawait(false); if (buffer.length == 0) { break; } totalbytesread += buffer.length; if (progress != null) progress.report(totalbytesread); await fs.writeasync(buffer).configureawait(false); } inputstream.dispose(); } return file.name; }
Comments
Post a Comment