简体   繁体   中英

The App hangs on a device while debugging async method

I'm following the guide at MSDN about downloading the file. So I have made a very simple download:

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)

The strange situation is that on Emulator everything works, but on the Device (Lumia 820) the code hangs every time when you debug it. If you set the breakpoint at the last line of DownloadWebFile - return bytes , it is being hit, shows correct number of bytes, you can step forward, but only to the bracket. When you try to step forward further, the App hangs (without breakpoints it also hangs). The file as I can see it via IsolatedStorageExplorer is downloaded correctly.

It seems that sometimes program hangs during debugging when trying to step out from async method (thanks @yasen)

Lately, when debugging on device, I cannot step out of async methods. If you have that problem, a simple workaround is to just skip these methods once you're sure they work correctly (don't go into them, don't put breakpoints in them).

Also, I haven't had such problems on the emulator, so if it's possible - just debug on it, instead of a device.

I don't know what's causing this, though. I'm pretty sure it used to work at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM