zoukankan      html  css  js  c++  java
  • 单线程读文件

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.IO;

    namespace ExToDB.FileTransfer
    {
        
    public class FileComsumer
        {
            
    private string fileName;
            
    private Stream streamOuput = null;
            
    private int bufferSize = 1024;

            
    private Thread fileThread = null;
            
    private volatile bool isStop;

            
    public event ReturnEndEvent OnReadEnd;

            
    public FileComsumer(string fileName)
            {
                isStop 
    = false;
                
    this.fileName = fileName;
            }

            
    /// <summary>
            
    /// 获取读到的Stream
            
    /// </summary>
            public Stream StreamOuput
            {
                
    get { return streamOuput; }
            }

            
    public void Start()
            {
                fileThread 
    = new Thread(new ThreadStart(TheadReadFile));
                fileThread.Start();
            }

            
    public void Stop()
            {
                isStop 
    = true;
                
    if (fileThread != null && fileThread.ThreadState == System.Threading.ThreadState.Running)
                    fileThread.Abort();
                fileThread 
    = null;
            }

            
    private byte[] buffer = null;

            
    /// <summary>
            
    /// 获取读到的数据
            
    /// </summary>
            public byte[] Buffer
            {
                
    get { return buffer; }
            }

            
    private void TheadReadFile()
            {
                streamOuput 
    = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, true);
                
    int numBytesToRead = (int)streamOuput.Length;
                
    int numBytesRead = 0;
                buffer 
    = new byte[streamOuput.Length];
                
    while (numBytesToRead > 0 && !isStop)
                {
                    
    // Read may return anything from 0 to numBytesToRead.
                    int n = streamOuput.Read(buffer, numBytesRead, numBytesToRead);
                    
    // The end of the file is reached.
                    if (n == 0)
                    {
                        OnOnReadEnd();
                        
    break;
                    }
                        numBytesRead 
    += n;
                    numBytesToRead 
    -= n;
                    
    //读取文件完成,触发事件
                    if (numBytesToRead == 0)
                        OnOnReadEnd();
                }
                streamOuput.Close();
            }

            
    private void OnOnReadEnd()
            {
                
    if(this.OnReadEnd!=null)
                    OnReadEnd(
    this,new ReturnEndReadEventargs(true));
            }
        }
    }
  • 相关阅读:
    NEC 框架规范 css reset
    NEC 工程师规范
    NEC html规范
    【bzoj2839】【集合计数】容斥原理+线性求阶乘逆元小技巧
    【bzoj1562】【[NOI2009]变换序列】匈牙利算法的性质利用
    【bzoj4808】【马】二分图最大点独立集+简单感性证明
    【hdu1150】【Machine Schedule】二分图最小点覆盖+简单感性证明
    【bzoj4950】【 [Wf2017]Mission Improbable】贪心+二分图匹配
    【bzoj4443】【[Scoi2015]小凸玩矩阵】二分+二分图最大匹配
    【bzoj1977】【严格次小生成树】倍增维护链上最大次大值
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1644490.html
Copyright © 2011-2022 走看看