线程的响应时间非常多(不常见)

时间:2015-04-09 22:05:29

标签: c# multithreading

我想使用Windows窗体应用程序中的线程在C#中下载文件,线程在第一次工作正常(持续5秒)但是突然它使其工作并且有时不响应大约10秒然后它返回第二个并且然后没有回应 我正在使用类下载文件 这是代码:

 class DownloadFile
{

    #region Fields

    static Double totalsize;
    Double received;
    int PartNum;
    long start, end;
    string savepath;
    HttpWebRequest request;

    Stream stream;
    #endregion

    #region Constructors
    public DownloadFile() { totalsize = 1; received = 0; }
    public DownloadFile(long s, long e, string PATH, HttpWebRequest REQUEST, int Partnumber)
    {
        totalsize = 1;
        PartNum = Partnumber;
        received = 0;
        start = s;
        request = REQUEST;
        end = e;
        savepath = PATH;

    }
    #endregion

    #region Methods
    public void DownLoad()
    {
        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        FileStream fstr = new FileStream(savepath + "Part_" + PartNum + Path.GetExtension(Request.RequestUri.ToString()), FileMode.Create, FileAccess.Write);
        HttpWebResponse response;
        this.request.AddRange(start, end);
        response = (HttpWebResponse)this.Request.GetResponse();
        stream = response.GetResponseStream();
        totalsize = this.end;
        bytesRead = stream.Read(buffer, 0, buffer.Length);
        while (bytesRead > 0)
        {
            received += bytesRead;
            fstr.Write(buffer, 0, bytesRead);
            bytesRead = stream.Read(buffer, 0, buffer.Length);
        }
        fstr.Close();
        stream.Close();
    }
    public void AppendFiles()
    {

    }
    #endregion

    #region properties
    public long End
    {
        get { return end; }
        set { end = value; }
    }

    public long Start
    {
        get { return start; }
        set { start = value; }
    }
    public Double Received
    {
        get { return received; }
    }
    public Double Totalsize
    {
        get { return totalsize; }
    }
    public HttpWebRequest Request
    {
        get { return request; }
        set { request = value; }
    }

    public string SavePath
    {
        get { return savepath; }
        set { savepath = value; }
    }
    #endregion

}

这里是线程开始的地方:

Class Program{
List<DownloadFile> DownLoadList = new List<DownloadFile>();
 public void DownloadMethod()
    {
        for (int i = 0; i < DownLoadList.Count; i++)
        {
            new Thread(new ThreadStart(DownLoadList.ElementAt(i).DownLoad)).Start();
        }

    }
}

我还有一个更新下载表单的计时器(progressBar和标签)

0 个答案:

没有答案