一个多线程下载程序报错,高手帮忙看看!!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;//support stream fuction
using System.Net;//support net fuction
using System.Threading;//support thread fuction
namespace http下载
{
public partial class Form1 : Form
{
public int iThreadCount;
public bool[] threadw;//mark of the thread end
public string[] filenamew;//recived filename
public int[] filestartw;//starting point of each thread
public int[] filesizew;//the file length of each thread received
public string strurl;//url
public bool hb;//
static public int m_iThreadCount;//thread number
public int icurrentfilesize;
static public int m_iTotalSize;
public int[] iCurrentFileSize;
public delegate void Prodelegate(int iValue);
public int setprogressbarvalue()
{
proMethod();
setProgressBarValue(icurrentfilesize);
return 0;
}
private void setProgressBarValue(int iValue)
{
try
{
if (this.progressBar1.InvokeRequired)
{ //如果进度条被访问
Prodelegate prodele = new Prodelegate(setProgressBarValue);
this.Invoke(prodele, new object[] { iValue });
}
else
{
this.progressBar1.Value = iValue;
}
}
catch { }
}
public void proMethod()
{
for (int i = 0; i < iThreadCount; i++)
{
icurrentfilesize += iCurrentFileSize[i];
}
}
/*
static public int SetprogressBar1Value(int iCurrentThreadID, int iThreadFileSize)
{
iCurrentThreadID = m_iCurrentThreadID;
m_iThreadSize[iCurrentThreadID] = iThreadFileSize;
m_iTotalSize = 0;
for (int i = 0; i < m_iThreadCount; i++)
{
m_iTotalSize += m_iThreadSize[m_iThreadCount];
}
progressBar1.Value = m_iTotalSize;
}
*/
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
download();
}
private void download()
{
strurl = textBox1.Text.Trim().ToString();
HttpWebRequest request;
long filesize = 0;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(strurl);
filesize = request.GetResponse().ContentLength;//get length of target document
m_iTotalSize = (int)filesize;
request.Abort();
}
catch (Exception er)
{
MessageBox.Show("get length of target document:" + er.Message);
}
//receiving thread number
m_iThreadCount = Convert.ToInt32(textBox3.Text.Trim().ToString(), 10);
//initial arrays According to thread number
threadw = new bool[m_iThreadCount];
filenamew = new string[m_iThreadCount];
filesizew = new int[m_iThreadCount];
filestartw = new int[m_iThreadCount];
// count file size of each thread should receiving
int filethread = (int)filesize / m_iThreadCount;//Average distribution
int filethreade = filethread + (int)filesize % m_iThreadCount;//
//evaluation
int i;
for (i = 0; i < m_iThreadCount; i++)
{
threadw[i] = false;//initial value:false
filenamew[i] = i.ToString() + ".dat";//Temporary filename
if (i < m_iThreadCount - 1)
{
filestartw[i] = filethread * i;//The starting point of each thread to receive files
filesizew[i] = filethread - 1;//The length of each thread received
}
else
{
filestartw[i] = filethread * i;//The starting point of each thread to receive files
filesizew[i] = filethreade - 1;//The file length of each thread receiving
}
}
/*
for (int iIndexThreadCount = 0; iIndexThreadCount < thread; iIndexThreadCount++)
{
m_downfilesize += filesizew[iIndexThreadCount];
}
*/
//Define arrays of threads,Start receiving thread
Thread[] threadk = new Thread[m_iThreadCount];
HttpFile[] httpfile = new HttpFile[m_iThreadCount];
int j;
for (j = 0; j < m_iThreadCount; j++)
{
httpfile[j] = new HttpFile(this, j);
threadk[j] = new Thread(new ThreadStart(httpfile[j].receive));
threadk[j].Start();
}
//start thread of merge
Thread hbth = new Thread(new ThreadStart(hbfile));
hbth.Start();
}
//merge file
public void hbfile()
{
while (true)//wait
{
hb = true;
for (int i = 0; i < m_iThreadCount; i++)
{
if (threadw[i] == false)//some threads have not end,waiting
{
hb = false;
Thread.Sleep(10);
break;
}
}
if (hb == true)
{
break;
}
}
FileStream fs;//begin to merge
FileStream fstemp;
int readfile;
byte[] bytes = new byte[512];
fs = new FileStream(textBox2.Text.Trim().ToString(), System.IO.FileMode.Create);
for (int k = 0; k < m_iThreadCount; k++)
{
fstemp = new FileStream(filenamew[k], FileMode.Open);
while (true)
{
readfile = fstemp.Read(bytes, 0, 512);
if (readfile > 0)
{
fs.Write(bytes, 0, readfile);
}
else
{
break;
}
}
fstemp.Close();
}
fs.Close();
MessageBox.Show("Receive complete!");
}
private void progressBar1_Validating(object sender, CancelEventArgs e)
{
}
}
public class HttpFile
{
public Form1 formm;
public int m_iCurrendThreadID;//thread number
public string filename;//filename
public string strurl;//URL
public FileStream fs;
public HttpWebRequest request;
public Stream ns;
public byte[] nbytes;//Receive buffer
public int nreadsize;//Receiving bytes
public HttpFile(Form1 form, int iThreadCount)
{
formm = form;
m_iCurrendThreadID = iThreadCount;
}
~HttpFile()
{
formm.Dispose();
}
public void receive()//receive thread
{
filename = formm.filenamew[m_iCurrendThreadID];
strurl = formm.strurl;
ns = null;
nbytes = new byte[512];
nreadsize = 0;
formm.listBox1.Items.Add("thread" + m_iCurrendThreadID.ToString() + "start receiving");
fs = new FileStream(filename, System.IO.FileMode.Create);
try
{
request = (HttpWebRequest)HttpWebRequest.Create(strurl);
request.AddRange(formm.filestartw[m_iCurrendThreadID], formm.filestartw[m_iCurrendThreadID] + formm.filesizew[m_iCurrendThreadID]);
ns = request.GetResponse().GetResponseStream();
nreadsize = ns.Read(nbytes, 0, 512);
while (nreadsize > 0)
{
fs.Write(nbytes, 0, nreadsize);
nreadsize = ns.Read(nbytes, 0, 512);
formm.listBox1.Items.Add("thread" + m_iCurrendThreadID.ToString() + "Receiving");
formm.iCurrentFileSize[m_iCurrendThreadID] = nreadsize;
}
fs.Close();
ns.Close();
}
catch (Exception er)
{
MessageBox.Show("thread error:"+er.Message);
fs.Close();
}
formm.listBox1.Items.Add("thread" + m_iCurrendThreadID.ToString() + "Receive complete!");
formm.threadw[m_iCurrendThreadID] = true;
}
}
}