Asynchronous File I/O

  • Thread starter Thread starter MSDN
  • Start date Start date
M

MSDN

Does anyone know how to do this with a readline statement or equivalent
method?

Thanks in advance.
Chris
 
Sample code...

Public Sub ReadFiles(ByVal path As String)

' make a reference to a directory

Dim di As New IO.DirectoryInfo(path)

Dim diar1 As IO.FileInfo() = di.GetFiles

Dim dra As IO.FileInfo

'list the names of all files in the specified directory

For Each dra In diar1

ReadData(dra.FullName)

Next

End Sub



Public Sub ReadData(ByVal Path As String)

'--This is the state object that will contain the file

'---and the return data. This class is nescessary because

'---we dont want the return data overwritten by each

'---read method (like it would if it was declared globally)

Dim tempStateObj As New StateObj

Try

tempStateObj.file = New System.IO.FileStream(Path, IO.FileMode.Open)

Catch ex As System.UnauthorizedAccessException

Exit Sub

Catch ex As System.IO.IOException

Exit Sub

End Try

'--Redimension the return data array to the appropriate length

ReDim tempStateObj.retData(Convert.ToInt32(tempStateObj.file.Length))

'--Call the Asynch read method. Pass the byte array we wish to fill

'---the offset, the length of the file to read, the address of the

'---callback sub, and finally a state object for our own use.

' MsgBox(Now & " Read Start: " & Path.Substring(InStrRev(Path, "\")))

tempStateObj.file.BeginRead(tempStateObj.retData, 0,
Convert.ToInt32(tempStateObj.file.Length), _

AddressOf OnReadDone, tempStateObj)





'--Control is immediately given back to the program. The BeginRead method

'---Does not block program execution like the Read method, so we are free to

'---get back to processing.



End Sub

Public Sub OnReadDone(ByVal ar As IAsyncResult)

'--This is the asynchronous callback delegate that is called when the
BeginRead

'---is done processing.

'--The state object is passed to us in ar. It is a generic

'---IAsyncResult, and must be casted into something usable

'---We know we passed a StateObj class, so we cast it as such

Dim state As StateObj = CType(ar.AsyncState, StateObj)

'--From our state object, we must call EndRead(ar) to get the

'---number of bytes read. Even if you do not wish to know the

'---number of bytes, you must *always* call EndRead in the callback

Dim bytesRecieved As Int32 = state.file.EndRead(ar)

MsgBox(Now & " Read Done: " & bytesRecieved)
'state.file.Name.Substring(InStrRev(state.file.Name, "\")))

'--If you don't wish to know the number of bytes read, do this:

'state.file.EndRead(ar)

state.file.Close()

'--Open up a new file to write to

'state.file = New System.IO.FileStream(Application.StartupPath & "\" &
state.file.Name.Substring(InStrRev(state.file.Name, "\")),
IO.FileMode.Create)

'--Begin the Asynch write to the file, passing everything and the state
object again

'state.file.BeginWrite(state.retData, 0, state.retData.Length, New
AsyncCallback(AddressOf OnWriteDone), state)



'--At this point, the sub will terminate and the thread will go back to the

'---internal threadpool. It is important not to do anything that will block
or take

'---large amounts of time in this sub. The internal threadpool is limited to

'---25 threads total, and it is important to return the thread as quick as
possible

'---to the pool for further use.

'Debug.WriteLine(Now & " Write Start: " &
state.file.Name.Substring(InStrRev(state.file.Name, "\")))

End Sub

Public Sub OnWriteDone(ByVal ar As IAsyncResult)

'--This is the asynchronous callback delegate that is called when the
BeginWrite

'---is done processing.

'--The state object is passed to us in ar. It is a generic

'---IAsyncResult, and must be casted into something usable

'---We know we passed a StateObj class, so we cast it as such

Dim state As StateObj = CType(ar.AsyncState, StateObj)

'--From our state object, we must call EndWrite(ar). You must

'---*always* call EndWrite in the callback

state.file.EndWrite(ar)

state.file.Close()

Debug.WriteLine(Now & " Write Done: " &
state.file.Name.Substring(InStrRev(state.file.Name, "\")))

'--At this point, the sub will terminate and the thread will go back to the

'---internal threadpool. It is important not to do anything that will block
or take

'---large amounts of time in this sub. The internal threadpool is limited to

'---25 threads total, and it is important to return the thread as quick as
possible

'---to the pool for further use.

End Sub
 
Back
Top