[英]The App hangs on a device while debugging async method
我正在遵循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)
奇怪的情況是,在Emulator上一切正常,但是在設備(Lumia 820)上,每次調試時代碼都會掛起。 如果將斷點設置在DownloadWebFile
的最后一行return bytes
,那么它將被命中,並顯示正確的字節數,您可以前進,但只能前進到括號。 當您嘗試進一步前進時,該應用程序將掛起(沒有斷點,它也將掛起)。 我通過IsolatedStorageExplorer看到的文件已正確下載。
似乎在嘗試從異步方法中退出時,有時程序會在調試過程中掛起(感謝@yasen)
最近,在設備上進行調試時,我無法退出異步方法。 如果您遇到問題,一個簡單的解決方法是在確定這些方法正確運行后就跳過這些方法(不要進入這些方法,不要在其中添加斷點)。
另外,我在仿真器上還沒有出現此類問題,因此,如果可能的話,只需在其上調試,而不是在設備上調試即可。
不過,我不知道是什么原因造成的。 我很確定它曾經在某個時候可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.