调试异步方法时,应用程序挂在设备上 [英] The App hangs on a device while debugging async method

查看:22
本文介绍了调试异步方法时,应用程序挂在设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 MSDN 上的指南 关于下载文件.所以我做了一个非常简单的下载:

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)

奇怪的情况是,在模拟器上一切正常,但在设备(Lumia 820)上,每次调试时代码都会挂起.如果在DownloadWebFile-return bytes的最后一行设置断点,则是命中,显示正确的字节数,可以前进,但只能到括号.当您尝试更进一步时,应用程序挂起(没有断点它也会挂起).我通过IsolatedStorageExplorer 看到的文件已正确下载.

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.

当试图退出异步方法时,似乎有时程序在调试过程中挂起(感谢@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.

这篇关于调试异步方法时,应用程序挂在设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆