UWP C# TryGetItemAsync - call hangs third time
Clash Royale CLAN TAG#URR8PPP
UWP C# TryGetItemAsync - call hangs third time
I'm pretty new in developing UWP apps and using await/async. I try getting a file from local folder of UWP app, but the app is hanging. The file exists on drive.
I wrote some test methods to try identifying the issue, but I don't understand, why it doesn't work. Two runs working fine and the third hangs always.
Has someone an idea, what I'm doing woring?
Code:
public void Test()
for (var i = 0; i < 10; i++)
KLogger.Log.Debug("");
KLogger.Log.Debug("### Started : " + i);
// create new or returns existent file
var task = TestGetNewFile("test.txt");
//task.ConfigureAwait(false);
task.Wait();
var res = task.Result;
task.Dispose();
KLogger.Log.Debug("### Done : " + i);
public async Task<IStorageFile> TestGetNewFile(string fileName)
var storageFolder = ApplicationData.Current.LocalFolder;
KLogger.Log.Debug("Creation started.");
var fileItem = await storageFolder.TryGetItemAsync(fileName);
KLogger.Log.Debug("Creation finished.");
return null;
Log Output:
2018-08-12 00:41:07.282 +02:00 [DBG]
2018-08-12 00:41:07.282 +02:00 [DBG] ### Started : 0
2018-08-12 00:41:07.284 +02:00 [DBG] Creation started.
2018-08-12 00:41:07.289 +02:00 [DBG] Creation finished.
2018-08-12 00:41:07.289 +02:00 [DBG] ### Done : 0
2018-08-12 00:41:07.289 +02:00 [DBG]
2018-08-12 00:41:07.289 +02:00 [DBG] ### Started : 1
2018-08-12 00:41:07.289 +02:00 [DBG] Creation started.
2018-08-12 00:41:07.300 +02:00 [DBG] Creation finished.
2018-08-12 00:41:07.300 +02:00 [DBG] ### Done : 1
2018-08-12 00:41:07.300 +02:00 [DBG]
2018-08-12 00:41:07.300 +02:00 [DBG] ### Started : 2
2018-08-12 00:41:07.300 +02:00 [DBG] Creation started.
-- nothing more in log file --
// Assembly location: C:Usersuser.nugetpackagesmicrosoft.netcore.universalwindowsplatform6.1.5refuap10.0.15138System.Runtime.dll - Task has a Dispose method. But you can remove the Dispose call. This was a test only. Without dispose, issue is the same.The UWP app is for target platform Windows 10, Version 1803 (10.0; Build 17134) Min. Version Windows 10 Fall Creators Update (10.0; Build 16xxx)
– Isgam
Aug 12 at 7:30
If you're calling this code from the UI thread, then it's surprising that it works the first two times, this is a known deadlock pattern. Try instead to call your method like this:
var task = Task.Run(() => TestGetNewFile("test.txt"));
– Kevin Gosse
Aug 12 at 8:38
var task = Task.Run(() => TestGetNewFile("test.txt"));
1 Answer
1
It works if you call it this way, use async-await
all the way down.
async-await
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
for (int i = 0; i < 10; i++)
var item = await TestGetNewFile("test.txt");
async Task<IStorageItem> TestGetNewFile(string fileName)
return await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);
You may find this answer await vs. Task.Wait - Deadlock? useful.
Hey! Great! This works perfect now with "async-await all the way down". Thanks a lot for your quick help!
– Isgam
Aug 12 at 23:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code does not compile. There is no Dispose method in Task class.
– kennyzx
Aug 12 at 3:50