CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    How to find if the file is busy

    I need to copy the file (OutboxLog.txt produced by Fax service) right after it was updated. I'm using:
    System.IO.File.Copy(strSource, strDestination, True) to do that, but if the source file is busy (was not closed yet after update) the copy process fails.
    How can I find if the file is still busy?
    Thank you
    Vlad

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: How to find if the file is busy

    When you do the copy, surround it with a Try...Catch block. If failed you will be able to see it in the exception message.

  3. #3
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: How to find if the file is busy

    Thank you.
    I do not need a message. I need to copy file in any case, but I don't know when it will be not busy.
    As a temporary solution I tried a loop:
    For lngAttempt = 0 To 100000
    Application.DoEvents()
    Next
    And it works. But I do not like this approach. I would prefer something like:
    Do While IsBusy = True
    IsBusy = some code telling the status of the file
    Loop

    Vlad

  4. #4
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: How to find if the file is busy

    I got a solution on another forum from Terry Olsen:
    Do
    Try
    file.copy(source,dest)
    Exit Do
    Catch
    End Try
    Loop

    It works!!!!

  5. #5
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: How to find if the file is busy

    After testing this approach within several days I realized that it doesn't work sometimes. Why, I have no idea. It just doesn't make any copy.
    Could anybody suggest anything for either copying the file or reading its contents when the file is in use?

    Thank you
    Vlad

  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: How to find if the file is busy

    Try something along these lines:

    Dim StillTrying as boolean = True

    Do While StillTrying
    Try
    File.Copy(Source, Dest)
    StillTrying = False
    Catch Ex as Exception
    System.Threading.Thread.CurrentThread.Sleep(SomeNumberOfMilliseconds)
    End Try
    Loop


    You might also want to add a counter for failed attempts and break the loop after ?? tries.

  7. #7
    Join Date
    Dec 2002
    Posts
    305

    Re: How to find if the file is busy

    What I do normally is this. I start a timer and set a boolean variable to false at the start of file update operation. After it has done updating and the source file has been closed, I set the boolean variable to false. Within the timer_tick sub, I check if the boolean variable; if it is true, I give additional second or two (using sleep command), then I copy the updated file to the dest file. Good luck.

  8. #8
    Join Date
    Dec 2002
    Posts
    305

    Re: How to find if the file is busy

    Another method I have used successfully is this: I start a timer when updating has started. Within the timer_tick routine, I check if a dummy file exists at every tick. At the end of file update, I create the dummy file and put one dummy line of info in it, save and close it. When the timer routine sees the file (if file.exists(dummy.dat) then...), the copy operation is started. After copy, dummy file is deleted, and timer stopped. Make sure you stop the timer. In the previous example, make sure you reset the boolean variable to false and stop timer after file.copy command. Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured