异步读取文件,读取完成之后触发事件,通知主线程。主线程与读取文件的线程互相独立。
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace ExToDB.FileTransfer
{
/// <summary>
/// 异步读取文件
/// </summary>
public class AsyncProcessor
{
private Stream inputStream;
public Stream InputStream
{
get { return inputStream; }
set { inputStream = value; }
}
// 每次读取块的大小
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; }
}
// 容纳接收数据的缓存
private byte[] buffer;
public AsyncProcessor(string fileName)
{
buffer = new byte[bufferSize];
// 打开文件,指定参数为true以提供对异步操作的支持
inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, true);
}
/// <summary>
/// 开始读取
/// </summary>
public void StartProcess()
{
// 开始异步读取文件,填充缓存区
inputStream.BeginRead(buffer, 0, buffer.Length, OnCompletedRead, null);
}
private void OnCompletedRead(IAsyncResult asyncResult)
{
// 已经异步读取一个 块 ,接收数据
int bytesRead = inputStream.EndRead(asyncResult);
// 如果没有读取任何字节,则流已达文件结尾
if (bytesRead > 0)
{
// 暂停以模拟对数据块的处理
Debug.WriteLine(" 异步线程:已读取一块");
Thread.Sleep(TimeSpan.FromMilliseconds(20));
// 开始读取下一块
inputStream.BeginRead(buffer, 0, buffer.Length, OnCompletedRead, null);
}
else
{
// 结束操作
Debug.WriteLine(" 异步线程:读取文件结束");
//读完,触发事件
OnIsReturnEvent();
//inputStream.Close();
}
}
}
public class ReturnEndReadEventargs : EventArgs
{
private bool isReturn;
public ReturnEndReadEventargs(bool isReturn)
{
this.isReturn = isReturn;
}
public bool IsReturn
{
get { return isReturn; }
set { isReturn = value; }
}
}
public delegate void ReturnEndEvent(object sender, ReturnEndReadEventargs args);
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace ExToDB.FileTransfer
{
/// <summary>
/// 异步读取文件
/// </summary>
public class AsyncProcessor
{
private Stream inputStream;
public Stream InputStream
{
get { return inputStream; }
set { inputStream = value; }
}
// 每次读取块的大小
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; }
}
// 容纳接收数据的缓存
private byte[] buffer;
public AsyncProcessor(string fileName)
{
buffer = new byte[bufferSize];
// 打开文件,指定参数为true以提供对异步操作的支持
inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, true);
}
/// <summary>
/// 开始读取
/// </summary>
public void StartProcess()
{
// 开始异步读取文件,填充缓存区
inputStream.BeginRead(buffer, 0, buffer.Length, OnCompletedRead, null);
}
private void OnCompletedRead(IAsyncResult asyncResult)
{
// 已经异步读取一个 块 ,接收数据
int bytesRead = inputStream.EndRead(asyncResult);
// 如果没有读取任何字节,则流已达文件结尾
if (bytesRead > 0)
{
// 暂停以模拟对数据块的处理
Debug.WriteLine(" 异步线程:已读取一块");
Thread.Sleep(TimeSpan.FromMilliseconds(20));
// 开始读取下一块
inputStream.BeginRead(buffer, 0, buffer.Length, OnCompletedRead, null);
}
else
{
// 结束操作
Debug.WriteLine(" 异步线程:读取文件结束");
//读完,触发事件
OnIsReturnEvent();
//inputStream.Close();
}
}
}
public class ReturnEndReadEventargs : EventArgs
{
private bool isReturn;
public ReturnEndReadEventargs(bool isReturn)
{
this.isReturn = isReturn;
}
public bool IsReturn
{
get { return isReturn; }
set { isReturn = value; }
}
}
public delegate void ReturnEndEvent(object sender, ReturnEndReadEventargs args);
}