locked
API to close the file in Metro style Apps RRS feed

  • Question

  • Hi

    --Using storageFolder.GetFileAsync(fileName) we get the file from storage folder. and then we append text into the file using FileIO.AppendTextAsync. 

    -- Please tell me by which method i can close the file in Metro style Apps which was modified by  FileIO.AppendTextAsync.

    -- Also tell me is there any way to call FileIO.AppendTextAsync method synchronously.


    Ravi Shankar

    Friday, June 29, 2012 4:37 AM

Answers

  • Hey,

    I am just assuming but from what I understood you code looks something like:

    (This would throw exception)

    private async void WriteMessage(StorageFile file, string message)
    {
        await FileIO.AppendTextAsync(file, message);
    }
    
    public async void TrialFunction() 
    {
        //... do something etc.
        StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("log.txt");
    
        for (var idx = 0; idx < 5; idx++)
        {
            WriteMessage(file, "Message" + idx);
        }
    }

    However, exactly as you explained, the Append is not finished yet before it goes into the second loop.

    What you might want to try:

    (This code would work since the write message is awaitable)

    private Task WriteMessage(StorageFile file, string message)
    {
        return FileIO.AppendTextAsync(file, message).AsTask();
    }
    
    public async void TrialFunction() 
    {
        //... do something etc.
        StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("log.txt");
    
        for (var idx = 0; idx < 5; idx++)
        {
            await WriteMessage(file, "Message" + idx);
        }
    }


    Can Bilgin
    Blog CompuSight

    Friday, June 29, 2012 11:29 AM

All replies

  • Theoretically it should close as soon as it's done appending the text.

    Why do you want to run AppTextAsync synchronously?

    Friday, June 29, 2012 4:58 AM
  • Hi.

    In our application i am writing the log into text file. To test this features i am calling

    await FileIO.AppendTextAsync(StorageFile, Message) from the loop, due to Async method it goes in background and returns call back to called function(loop) and second call raised before completion of first AppendTextAsync.

    Regards 


    Ravi Shankar

    Friday, June 29, 2012 5:20 AM
  • Hey,

    I am just assuming but from what I understood you code looks something like:

    (This would throw exception)

    private async void WriteMessage(StorageFile file, string message)
    {
        await FileIO.AppendTextAsync(file, message);
    }
    
    public async void TrialFunction() 
    {
        //... do something etc.
        StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("log.txt");
    
        for (var idx = 0; idx < 5; idx++)
        {
            WriteMessage(file, "Message" + idx);
        }
    }

    However, exactly as you explained, the Append is not finished yet before it goes into the second loop.

    What you might want to try:

    (This code would work since the write message is awaitable)

    private Task WriteMessage(StorageFile file, string message)
    {
        return FileIO.AppendTextAsync(file, message).AsTask();
    }
    
    public async void TrialFunction() 
    {
        //... do something etc.
        StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("log.txt");
    
        for (var idx = 0; idx < 5; idx++)
        {
            await WriteMessage(file, "Message" + idx);
        }
    }


    Can Bilgin
    Blog CompuSight

    Friday, June 29, 2012 11:29 AM
  • Thanks a lot. now problem has been resolved.

    Ravi Shankar

    Friday, June 29, 2012 2:05 PM
  • Hi Can Bilgin

    Now, problem is that we have to make all method async which calls the

    WriteMessage(StorageFile file, string message) 

    .

    Regards


    Ravi Shankar

    Monday, July 2, 2012 7:33 AM
  • yeap unfortunately, that is correct...

    Can Bilgin
    Blog CompuSight

    Monday, July 2, 2012 7:41 AM