简体   繁体   中英

CreateFileAsync() stops debugging and never returned

Now I'm trying to make UWP application with VS2017 using C#. I use CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists) to create two files at the initial time, to open them afterwards. However it stopped during opening the second file (F5, F10, F11 buttons doesn't work during debugging). It runs cool during Creating files or opening the first file. Here is my code.

public static StorageFile GetDataFileFromLocalFolder(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
        {
            throw new ArgumentNullException("fileName missing");
        }

        return GetDataFileFromLocalFolderAsync(fileName).Result;
    }

    private static async Task<StorageFile> GetDataFileFromLocalFolderAsync(string fileName)
    {
        var sFolder = ApplicationData.Current.LocalFolder;
        var sFile = await sFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
        return sFile;
    }

Is there any problem? Shouldn't use OpenIfExist option?

I solve this problem as avoiding await keyword.

public static async Task<StorageFile> GetDataFileFromLocalFolderAsync(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
        {
            throw new ArgumentNullException("fileName missing"); //remove hard coded string here
        }

        var sFolder = ApplicationData.Current.LocalFolder;
        var sFileTask = sFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
        var sFile = sFileTask.AsTask().Result;

        return sFile;
    }

Now it works fine. but never knows why....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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