Asynchronous File I/O

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MSDN

    #1

    Asynchronous File I/O

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

    Thanks in advance.
    Chris


  • MSDN

    #2
    Re: Asynchronous File I/O

    Sample code...

    Public Sub ReadFiles(ByVal path As String)

    ' make a reference to a directory

    Dim di As New IO.DirectoryInf o(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.Fu llName)

    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.fi le = New System.IO.FileS tream(Path, IO.FileMode.Ope n)

    Catch ex As System.Unauthor izedAccessExcep tion

    Exit Sub

    Catch ex As System.IO.IOExc eption

    Exit Sub

    End Try

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

    ReDim tempStateObj.re tData(Convert.T oInt32(tempStat eObj.file.Lengt h))

    '--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.fi le.BeginRead(te mpStateObj.retD ata, 0,
    Convert.ToInt32 (tempStateObj.f ile.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(ByVa l 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.AsyncS tate, 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.EndR ead(ar)

    MsgBox(Now & " Read Done: " & bytesRecieved)
    'state.file.Nam e.Substring(InS trRev(state.fil e.Name, "\")))

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

    'state.file.End Read(ar)

    state.file.Clos e()

    '--Open up a new file to write to

    'state.file = New System.IO.FileS tream(Applicati on.StartupPath & "\" &
    state.file.Name .Substring(InSt rRev(state.file .Name, "\")),
    IO.FileMode.Cre ate)

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

    'state.file.Beg inWrite(state.r etData, 0, state.retData.L ength, New
    AsyncCallback(A ddressOf 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.WriteLin e(Now & " Write Start: " &
    state.file.Name .Substring(InSt rRev(state.file .Name, "\")))

    End Sub

    Public Sub OnWriteDone(ByV al 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.AsyncS tate, StateObj)

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

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

    state.file.EndW rite(ar)

    state.file.Clos e()

    Debug.WriteLine (Now & " Write Done: " &
    state.file.Name .Substring(InSt rRev(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

    "MSDN" <calhoun_chris@ hotmail.com> wrote in message
    news:%23lhwXaCd GHA.4264@TK2MSF TNGP05.phx.gbl. ..[color=blue]
    > Does anyone know how to do this with a readline statement or equivalent
    > method?
    >
    > Thanks in advance.
    > Chris
    >[/color]


    Comment

    Working...