none
[UWP] WriteTextAsync file used by another process RRS feed

  • Question

  • Hello, 

    I'm building an app using javascript and writing text to a file frequently. I'm using FileIO.writeTextAsync and get this error: The process cannot access the file because it is being used by another process. I'm sure it's trying to write while another one is already writing to the file but I'm not sure the best way to solve this issue. Any ideas?

    Thanks,

    Alan


    • Edited by Alan____C Thursday, May 26, 2016 7:54 PM
    Tuesday, May 24, 2016 9:40 PM

Answers

All replies

  • Hi Alan_C,

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    Could you tell us what type is your APP? Is it “Universal Windows Phone 8.1 apps” or “Windows 8.1 apps” or “Windows 10 universal app”?

    >>" I'm sure it's trying to write while another one is already writing to the file but I'm not sure the best way to solve this issue. "

    Could you please share some related code? Or just share a demo project with us. In this way it will help us analyze this issue more clearly.

    >>The process cannot access the file because it is being used by another process.

    We can use SemaphoreSlim Class to act as an async-compatible lock, after that we can ensure that the file is being accessed by only one thread at a time, for example:

            SemaphoreSlim sSlim = new SemaphoreSlim(1);
            async Task MyWtireFileMethod()
            {
                await sSlim.WaitAsync();
                try
                {
                    //Your write file code...
                }
                finally
                {
                    sSlim.Release();
                }
            }

    More details, you could refer to this similar question: http://stackoverflow.com/questions/17000227/metro-app-fileio-writetextasync-multiple-threads

    Best Regards,

    Leon Guang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, May 26, 2016 6:16 AM
  • Hi Leon thanks for the response. I'm creating a Windows 10 universal app using javascript. I want to open a file (create if it doesn't exist), read the contents, alter them, and write back to the file. I saw the SemaphoreSlim class but it seems it's not supported in javascript. I'd also like to queue any requests to the write so they don't get ignored if it's in use.

    Here's some code. I'm using es6 that gets transpiled:

    setItem: (key = '', value = '') => {
                return new Promise ((resolve, reject) => {
                    root.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.openIfExists).then(file => {
                        Windows.Storage.FileIO.readTextAsync(file).done(data => {
                            try {
                                let settings = {};
                                if (data !== '') {
                                    settings = JSON.parse(data);
                                }
                                settings[key] = value;

                                Windows.Storage.FileIO.writeTextAsync(file, JSON.stringify(settings)).done(() => {
                                    resolve();
                                });
                            }

                            catch (e) {
                                reject(e);
                            }
                        });
                    });
                });
            },

    Thanks!

    Alan


    Thursday, May 26, 2016 8:14 PM
  • I ended up using an async-lock package from npm. Seems to work for now
    • Marked as answer by Alan____C Tuesday, May 31, 2016 4:48 PM
    Tuesday, May 31, 2016 4:48 PM