Akash
Akash

Reputation: 1726

Unable to step through foreach loop

private async void Clicked(object sender, RoutedEventArgs e)
{
    StorageFolder s;
    s = KnownFolders.DocumentsLibrary;
    IReadOnlyList<StorageFile> l = await s.GetFilesAsync();
    bool exists=false;
    foreach (StorageFile sf in l)
    {
        if (string.Equals(sf.Name, "encrypted.txt", StringComparison.CurrentCultureIgnoreCase))
            exists = true;
    }
    MessageDialog d = new MessageDialog(exists.ToString());
    await d.ShowAsync();
}

When debugging the code, I'm not able to step through the code inside the foreach loop

While the value of exists at the end of the loop is correct, and I can see the loop execution if I put the MessageDialog code inside the loop, any idea how to step into the loop properly?

Even when I put the MessageDialog code in the loop, I'm unable to step into the if condition, so I suspect the issue lies somewhere in there

EDIT: Putting a breakpoint on the if condition works (currently I'm putting it on the 1st line of the function), but shouldn't I be able to step into the loop normally using F11 if I'm debugging line by line anyways? (atleast that's how it worked in TurboC)

EDIT2: Easiest way I could think of to show the issue clearly: http://www.youtube.com/watch?v=10GgXCqLlVo&feature=youtu.be (skip to 25 second mark to see the actual issue)

Upvotes: 2

Views: 1406

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499770

I don't know the details of how debugging works with async methods (I don't tend to use debuggers much), but you need to understand that the system will basically call back into the method, having returned from it (as far as the caller is concerned) when it first reaches an await expression which hasn't already completed.

I would put a break point on the bool exists=false; line, i.e. after the first await expression. You should then be able to use F10 (step over) to iterate through the loop.

Alternatively, you could get rid of the loop entirely using LINQ:

bool exists = l.Any(sf => string.Equals(sf.Name, "encrypted.txt",
                              StringComparison.CurrentCultureIgnoreCase));

Upvotes: 1

Related Questions