• 单线程读文件


    代码
    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));
            }
        }
    }
  • 相关阅读:
    LoadRunner FAQ
    LoadRunner下载文件脚本
    Loadrunner上传文件解决办法(大文件)
    LoadRunner中的IP欺骗的设置以及误区
    Loadrunner11在win7下录制脚本,ie打不开
    LoadRunner检查点实战
    设置Loadrunner负载机临时文件目录
    ArcGIS中国工具,版权声明,本人没有授权任何单位和个人销售,其他都是盗版,为了你个人和单位利益,请勿购买。 销售QQ:27652980,853740877,电话:18987281928,13108507190,qq群310964401
    勘测定界软件,增加平方米和公顷,小数进位面积不平,自动调平功能;不够线状扣除负面积自动处理功能 ​​​​
    arcgis 获得工具箱工具的个数
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1644490.html
Copyright © 2020-2023  润新知