VB.Net/File Directory/Stream Action

Материал из VB Эксперт
Перейти к: навигация, поиск

Register Stream Reading Complete Action Listener

Imports System
 Imports System.IO
 Imports System.Text
Public Class MainClass
    Dim Shared inputStream As Stream = New FileStream("test.txt",FileMode.Open,FileAccess.Read, _
                                       FileShare.ReadWrite,1024, True)
    Dim Shared myCallBack As AsyncCallback
    Dim Shared BufferSize As Integer = 256
    Dim Shared buffer(  ) As Byte = New Byte(BufferSize) {}
    Shared Sub Main(  )
         myCallBack = AddressOf OnCompletedRead
         inputStream.BeginRead(buffer,0,buffer.Length,myCallBack, Nothing)
         Dim i As Long
         For i = 0 To 499999
             If i Mod 1000 = 0 Then
                 Console.WriteLine("i: {0}", i)
             End If
         Next i
    End Sub
    Shared Sub OnCompletedRead(ByVal asyncResult As IAsyncResult)
        Dim bytesRead As Integer = inputStream.EndRead(asyncResult)
        If bytesRead > 0 Then
            Dim s As String = _
              Encoding.ASCII.GetString(buffer, 0, bytesRead)
            Console.WriteLine("OnComplete" &  s)
            inputStream.BeginRead( _
               buffer, 0, buffer.Length, myCallBack, Nothing)
        End If
    End Sub
   
End Class