0

In vb.net, I have my program run sqlite3.exe. Sqlite3 runs the following commands, which are stored in a text file:

.open prod.db
.mode csv
.output output.csv
SELECT STATEMENT
.output stdout
.exit

Once output.csv has been created, I then run a Streamreader to see what the results were and I get the following error:

The process cannot access the file 'output.csv' because it is being used by another process.

However, I know it is not locked up for long because I can go to the csv and open it manually after it is created.

I would write code to check to see when the process finishes, but I'm not sure what process is running to check this (I already check for SQLite3.exe to close). In other words, how do I know my csv is free for Streamreader to open?

Dustin
  • 462
  • 1
  • 6
  • 19
  • Is there a way to have that return a code at the end, line number of lines so that your code would have to wait for the result? (not a fan of, or familiar with SQLite) – Ňɏssa Pøngjǣrdenlarp Mar 31 '16 at 18:24
  • 1
    Check [this](http://stackoverflow.com/a/5380257/6141544) answer by Manuel Alves. – droque Mar 31 '16 at 18:42
  • Perfect! I was able to use this and check the csv rather than the process (which, I'm ashamed to admit, I did not think to do...!). – Dustin Mar 31 '16 at 18:54

1 Answers1

0

Using droque's link"

VB.Net 3.5 Check if file is in use

I was able to combine this with some of my own code to fix the problem:

While FileInUse("output.csv") And j < 100
        System.Threading.Thread.Sleep(100)
            j += 1
End While
'Timeout after ~10 seconds
If j = 100 Then
        MessageBox.Show("CSV timeout.  Exiting now.")
        Exit Sub
End If

where FileInUse is defined here:

Public Function FileInUse(ByVal sFile As String) As Boolean
        Dim thisFileInUse As Boolean = False
        If System.IO.File.Exists(sFile) Then
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    ' thisFileInUse = False
                End Using
            Catch
                thisFileInUse = True
            End Try
        End If
        Return thisFileInUse
    End Function
Community
  • 1
  • 1
Dustin
  • 462
  • 1
  • 6
  • 19