locked
How to open file in sync?

    Question

  • 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

    Wednesday, August 8, 2012 6:04 PM

Answers

  • 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

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Thursday, August 9, 2012 12:12 PM
  • 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.
    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Thursday, August 9, 2012 4:09 PM
  • Disable the button when it is clicked and then reenable it when all of the async handling is finished.

    --Rob

    • Proposed as answer by Jesse Jiang Tuesday, August 14, 2012 7:14 AM
    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Friday, August 10, 2012 6:01 PM
    Owner

All replies

  • 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.
    Wednesday, August 8, 2012 7:35 PM
  • 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

    Wednesday, August 8, 2012 8:04 PM
  • I cannot use standard Win32 CreateFile(). There is a limitation for Win RT to access file in Documents folder.

    Charlie Chang L

    Thursday, August 9, 2012 1:59 AM
  • 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

    Thursday, August 9, 2012 2:10 AM
  • 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

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Thursday, August 9, 2012 12:12 PM
  • 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.
    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Thursday, August 9, 2012 4:09 PM
  • 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

    Friday, August 10, 2012 1:19 PM
  • 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

    Friday, August 10, 2012 2:47 PM
  • Disable the button when it is clicked and then reenable it when all of the async handling is finished.

    --Rob

    • Proposed as answer by Jesse Jiang Tuesday, August 14, 2012 7:14 AM
    • Marked as answer by Jesse Jiang Friday, August 24, 2012 7:01 AM
    Friday, August 10, 2012 6:01 PM
    Owner