我正在遵循MSDN 上关于下载文件的指南。所以我做了一个非常简单的下载:
private async void performDownload_Click(object sender, RoutedEventArgs e)
{
CancellationTokenSource myCts = new CancellationTokenSource();
ulong bytesReceived = await DownloadWebFile("myFile.jpg", myCts.Token);
var forBreakpoint = 5; // set breakpoint here - isn't being hit on a device
// some further code
}
public async Task<ulong> DownloadWebFile(string fileName, CancellationToken ct)
{
Uri requestUri = new Uri("http://photojournal.jpl.nasa.gov/jpeg/PIA17555.jpg");
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
BackgroundDownloader downloader = new BackgroundDownloader();
downloader.Method = "GET";
downloader.CostPolicy = BackgroundTransferCostPolicy.Always;
DownloadOperation operation = downloader.CreateDownload(requestUri, file);
operation.Priority = BackgroundTransferPriority.High;
await operation.StartAsync().AsTask(ct);
ulong bytes = operation.Progress.BytesReceived;
return bytes; // set breakpoint here - it is being hit
} // here App hangs (only on Device, on emulator works)
奇怪的情况是,在模拟器上一切正常,但在设备(Lumia 820)上,每次调试时代码都会挂起。如果您在DownloadWebFile
-的最后一行设置断点return bytes
,它正在被命中,显示正确的字节数,您可以前进,但只能到括号。当您尝试进一步前进时,应用程序会挂起(没有断点它也会挂起)。我可以通过 IsolatedStorageExplorer 看到的文件已正确下载。
似乎有时程序在尝试退出异步方法时在调试期间挂起(感谢@yasen)