简体   繁体   English

调试异步方法时,App挂在设备上

[英]The App hangs on a device while debugging async method

I'm following the guide at MSDN about downloading the file. 我正在遵循MSDN上有关下载文件的指南 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. 奇怪的情况是,在Emulator上一切正常,但是在设备(Lumia 820)上,每次调试时代码都会挂起。 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. 如果将断点设置在DownloadWebFile的最后一行return bytes ,那么它将被命中,并显示正确的字节数,您可以前进,但只能前进到括号。 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. 我通过IsolatedStorageExplorer看到的文件已正确下载。

It seems that sometimes program hangs during debugging when trying to step out from async method (thanks @yasen) 似乎在尝试从异步方法中退出时,有时程序会在调试过程中挂起(感谢@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. 我很确定它曾经在某个时候可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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