Waiting Until CreateFile can Access File

This is a discussion on Waiting Until CreateFile can Access File within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to write to a file using CreateFile and WriteFile. However, I want to be able to wait ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    88

    Waiting Until CreateFile can Access File

    I am trying to write to a file using CreateFile and WriteFile. However, I want to be able to wait until the file is available for access. If another app(some other app I did not write) is accessing it, I want to wait until its access is not restricting CreateFile from successfully opening the file. Is there any way to do this?

    (I know it can be accomplished with a sleep-check loop, but I am trying to find a more graceful way of handling it. I am hoping for some windows event notification method).

    Thank you.

    Joe

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well I can't remove those annoying double-spaces in the code...
    Code:
    while(CreateFile("filename.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)==INVALID_HANDLE_VALUE){
    }
    dosomethinghere

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Thanks for the suggestion, but like I said in my original post I want to avoid a sleep-check loop. I want something notification based so my program does not busy wait.

    Joe

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Look at

    FindFirstChangeNotification()

    or

    ReadDirectoryChangesW()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Thanks for the suggestion. I already was familiar with the API functions, but they will not work if the open handle is a read only handle (as far as I can tell). Since reading the file would block me from opening it, and reading the file would not cause any change in file size/attributes/etc, it seems like I would not receive notification of when it was done reading. Does FILE_NOTIFY_CHANGE_LAST_ACCESS get modified when the file handle is released? If it is, that technique may work.

    Joe

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you don't mind using multithreaded solution, you can do it something like this:
    Put this at the beginning of the file:
    Code:
    #include <process.h>
    void name(void *P);
    Put this where you want to start trying to create the file:
    Code:
    _beginthread(name,0,NULL);
    And you can put this to the end of the file then:
    Code:
    void name(void *P){
    while(CreateFile("filename.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_  SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMA  L,NULL)==INVALID_HANDLE_VALUE){
    }
    dosomethingornotifytherestoftheprogram
    }
    This can be very succesfully used with WIN32 GUI applications.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Maxorater, that is a busy waiting method. Without a sleep in there, it will consume 99% of your CPU cycles. With a sleep in there, it will consume less but it is always a balance between trying to get a timely notification and not eating up the CPU time unnecessarily. I know the method works, I am just looking for something more graceful. Thanks for the suggestion though.

    Joe

  8. #8
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    That's about it, really. Windows only provides for situations where the file is gonna change (as the result of a write operation) rather than just reading.

    If you're a masochist, you could write a kernel shim that mimics the CreateFile/CloseHandle functions and notifies you when things go on with them.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Smurf,

    Yeah the shim would work, but more effort than it is worth(and more performance detriment as well) as you are suggesting. The FILE_NOTIFY_CHANGE_LAST_ACCESS may work. I will have to try it out when I get a chance. What really suprises me is how seemingly valuable this type of notification would be, and yet Windows does not provide it.

    Thank you for the suggestion. I will post the results of my test when I complete them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21