locked
how to know a file is ready to be opened RRS feed

  • Question

  • In the project I am working on, I need to start a process to do a task. This task will create a text file which i will use it immediately after it. But sometimes the runtime will issue an IOexception, stating that the text file can not be accessed as it is currently used by another process.

    How can i check if a text file is ready to be accessed?
    Monday, July 16, 2007 2:23 AM

Answers

  •  

    I really don't think you can... You'll have to open the file in a try - catch block in a timer event.

     

    Timer event handler

     

    Try

        streamreaderMemberVariable = new streamreader(filename)

        timer.stop

        timer = nothing

    Catch

        return

    Finally

       if timer is Nothing then ProcessFile ' Routine to process the file using streamreader
                                                                                                              'member  variable

       return

    End try

      

     I'd recommend an interval of about 500 ms on the timer.

    Monday, July 16, 2007 3:08 AM

All replies

  •  

    I really don't think you can... You'll have to open the file in a try - catch block in a timer event.

     

    Timer event handler

     

    Try

        streamreaderMemberVariable = new streamreader(filename)

        timer.stop

        timer = nothing

    Catch

        return

    Finally

       if timer is Nothing then ProcessFile ' Routine to process the file using streamreader
                                                                                                              'member  variable

       return

    End try

      

     I'd recommend an interval of about 500 ms on the timer.

    Monday, July 16, 2007 3:08 AM
  • can I use Thread.sleep(###)? to let the process finish its job, and make the text file available for access? I know this is a not a bullet proof solution, but it should work most of the time, does it?


    Monday, July 16, 2007 2:07 PM
  •  

    NEVER use Sleep.  Ever.....  Windows is an event driven system. Sleep is a non-event and it really does put a process to sleep and make it unresponsive to every thing.

     

    What I suggest will work.

    Monday, July 16, 2007 3:07 PM
  • I had a simular problem where I needed to wait untill after a process had ended this is what I did seemed to work . I needed to wait a bit for some output from the process to complete .

     

    Code Snippet

     

    Public Class Form1

    Dim appdone As Boolean = False

     

    Code Snippet

    Dim app As Process = Process.Start(startInfo)

    app.EnableRaisingEvents = True

    AddHandler app.OutputDataReceived, (AddressOf OnProcessConsoleOutput)

    AddHandler app.Exited, (AddressOf appexit)

    app.BeginOutputReadLine()

     

    While (appdone = False)

    My.Application.DoEvents()

    End While

     

    appdone = False

     

    Code Snippet

    Private Sub appexit(ByVal sender As Object, _

    ByVal e As System.EventArgs)

    Thread.Sleep(2000)

    appdone = True

    End Sub

     

    Monday, July 16, 2007 3:47 PM
  • Got to agree her with ReneeC.   Thread.sleep is often touted as a solution to pausing issues here and is about the worst solution possible in most cases.   Thread.sleep effectively suspends all processing on the thread period.   As an example if you put a threading.sleep to pause for lets say 5 seconds, everything will stop for 5 seconds with you application.  So if you try clicking a button on the form or opening another form in your application nothing will happen in that 5 second period within your application.

     

    Using events such as timers is a better solution as it allows your application to remain responsive while it is waiting.   So lets say during that 5 second you wish to wait, you could click a button to cancel or close your application.   Using events your User Interface remains active.

     

     

    Monday, July 16, 2007 5:01 PM
  • I don't know why VB does not have a pause or wait command it would be usefull . In my example my process was a console app . The output was put into a richtextbox but it took time for that to happen after the process exited . I put some text of my own into the richtextbox that was to go after all the output from the process but that is not the way it was because of the time it took for the last output of the process to be written into the richtextbox . A delay was needed .
    Tuesday, July 17, 2007 8:54 AM
  •  

    The operating system I used to work on Had a proces state Hibernate. It was really useful as execution stopped however Asyncronous traps, I/O and timers could be delivered. It was a frequent state that engineers used for servers etc. There doesn't appear to be anything like that. But I've come to using sleep to be an indication of a basic design error. I've written more than 100,000 lines of dot net and I've only used sleep once. It was for a toolbar that glides down. So microscopically it moves a couple of pixels and sleeps a few milliseconds until it wakes and moves another couple of pixels. It does exactly what I want. It freezes for a couple of milliseconds. That's the only valid use I've seen for sleep and it was in the middle of code with a lot of context. I would not use sleep in any serious application.

     

    Spotty, I think we need to do a thread on why not use sleep and alternatives and I'd love for you, I, Nobugs and SJ to participate in that.

    Tuesday, July 17, 2007 12:59 PM