Pergunta

Hello there is something very strange happening here, i have these lines of code working perfectly in another testing solution but when i have them in my main solution i have a problem:

When i run it normally, it doesnt do anything, when i run in Debug and go step by step then it works.

private async void InitializeContents()
    {
        var path = @"Assets\Data\";
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        try
        {
            var file = await folder.GetFileAsync(path + "test.txt");
            var lines = await FileIO.ReadLinesAsync(file);
            var pref = "";
            foreach (string line in lines)
            {                                       
                if (line.ElementAt(0).Equals('-'))
                {
                    switch (line.ElementAt(1))
                    {
                        case 'c': { pref = @"City\";break; }
                        case 'v': { pref = @"Village\"; break; }
                    }
                    continue;
                }
                file = await folder.GetFileAsync(path + pref + line);
                var sub_lines = await FileIO.ReadLinesAsync(file);
            }

        }
        catch (FileNotFoundException)
        {
        }
Foi útil?

Solução

You are probably not seeing the results because you're not waiting for InitializeContents to complete. Follow the Task-based Asynchronous Pattern (TAP); I also have an async intro on my blog you may find helpful.

If my speculation is correct, then you can fix the code by making InitializeContents return Task instead of void (and also rename it to follow the TAP naming convention: InitializeContentsAsync), and have the calling code await the Task that is returned from InitializeContentsAsync.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top