Answered How to open file in sync?

  • mercoledì 8 agosto 2012 18:04
     
     

    By using file->OpenAsync( ... ) the file was opened in async.

    But I want to open a file like the OpenFile(...) in Windows 7 to open a file in sync, that is I must be waiting for the file opened and go ahead to next step. How can I do this in Metro?

     


    Charlie Chang L

Tutte le risposte

  • mercoledì 8 agosto 2012 19:35
     
     
    You can use the standard C fopen/fread/fwrite/etc functions to handle files synchronously. The Win32 CreateFile2 API also works (see SimpleBlockReader.cpp in the samples). Regardless of which route you take, you'll still need to get the directory paths from WinRT.
  • mercoledì 8 agosto 2012 20:04
     
     

    You may also be able to use PPL, which lets you asynchronously open the file then schedule remaining work to continue after the async operation is complete.

    See this topic on async programming in C++ for details:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh780559.aspx

  • giovedì 9 agosto 2012 01:59
     
     
    I cannot use standard Win32 CreateFile(). There is a limitation for Win RT to access file in Documents folder.

    Charlie Chang L

  • giovedì 9 agosto 2012 02:10
     
     

    I read the sample to use PPL but cannot find the code.

    What I want to do is waiting the complete of opening a local file because I need this file to be ready to go.

    The instant return of OpenAsync() makes my program in wrong sequence and crash.

    If Metro API is complete it should have a function to do waiting. There is no problem in C# code but there is trouble in C++.


    Charlie Chang L

  • giovedì 9 agosto 2012 12:12
    Moderatore
     
     Con risposta

    Hello,

    I would suggest you to follow this sample codes.
    The way to open file is different from Win32, also different from C#, in Windows 8 style app.
    File access sample

    http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

  • giovedì 9 agosto 2012 16:09
     
     Con risposta Contiene codice

    You can make OpenAsync( ... )  synchronous by using event and WaitForSingleObjectEx() like

    task<void>( OpenAsync( ... )  ).then([this] (task<void> previousTask) {
            try{ 
        SetEvent(htest); //Success   
    }
    catch(Exception e) {    
       SetEvent(htest); //Failure 
       }
    
    WaitForSingleObjectEx(htest, INFINITE, TRUE);
    You should run this sync function in seperate worker thread in WinRT, bccause UI thread does not expect any block.
  • venerdì 10 agosto 2012 13:19
     
     

    Hi Charlie,

    Please have look Asynchronous programming(Metro Style app).

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx
    As per Metro style any time consuming operations shoulld written in asynchronous style.

    •Working with the file system is such operation.

    So its better you to go up with Async  method.

    thanks,
    Bhash

  • venerdì 10 agosto 2012 14:47
     
     

    Please have look Asynchronous programming(Metro Style app).

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx
    As per Metro style any time consuming operations shoulld written in asynchronous style.

    •Working with the file system is such operation.

    Actually I applied async style now. But it caused crash in random. When I click a button, three files need to be read and handled. In async style I am free to click the button again. I can click quickly before the complete of file operations. This kind of reentry may cause wrong logic. Without sync file reading how can I solve reentry problem?


    Charlie Chang L

  • venerdì 10 agosto 2012 18:01
    Moderatore
     
     Con risposta

    Disable the button when it is clicked and then reenable it when all of the async handling is finished.

    --Rob