Asynchronously reads files, triggers an event after reading is complete, and notifies the main thread. The main thread and the reading thread are independent of each other.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Diagnostics;
Using System. Threading;
Namespace ExToDB. FileTransfer
{
/// <Summary>
/// Read files Asynchronously
/// </Summary>
Public class AsyncProcessor
{
Private Stream inputStream;
Public Stream InputStream
{
Get {return inputStream ;}
Set {inputStream = value ;}
}
// The size of each read Block
Private int bufferSize = 2048;
Public event ReturnEndEvent IsReturnEvent;
Protected void OnIsReturnEvent ()
{
If (IsReturnEvent! = Null)
IsReturnEvent (this, new ReturnEndReadEventargs (true ));
}
Public int BufferSize
{
Get {return bufferSize ;}
Set {bufferSize = value ;}
}
// Supports the cache of received data
Private byte [] buffer;
Public AsyncProcessor (string fileName)
{
Buffer = new byte [bufferSize];
// Open the file and specify the parameter to true to support asynchronous operations
InputStream = new FileStream (fileName, FileMode. Open, FileAccess. Read, FileShare. Read, bufferSize, true );
}
/// <Summary>
/// Start reading
/// </Sum