一个多线程下载程序报错,高手帮忙看看!!!!

riyuehuan 2010-09-12 11:52:56
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;

}




}

}
...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
riyuehuan 2010-09-12
  • 打赏
  • 举报
回复
object refrence not set to an instance of an object
riyuehuan 2010-09-12
  • 打赏
  • 举报
回复

这个错误
wuyq11 2010-09-12
  • 打赏
  • 举报
回复
提示什么错误
内容概要:本文系统研究了构网型变流器的正负序阻抗解耦特性及其在弱电网环境下的稳定性表现,重点依托Matlab/Simulink仿真平台,构建了详细的阻抗数学模型,设计了解耦控制策略,并采用小信号扫频法进行频域辨识与稳定性验证。研究深入探讨了构网型变流器与传统跟网型逆变器在正负序阻抗特性上的本质差异,结合虚拟同步发电机(VSG)等先进控制技术,分析其在抑制宽频带振荡、削弱锁相环动态耦合等方面的优越性。文中不仅提供了完整的仿真模型与MATLAB代码实现,还整合了光伏、风电、储能、微电网等多类新能源系统的阻抗建模与稳定性分析资源,形成了一套面向新型电力系统稳定性的综合性技术资料体系,具有较强的科研复现与工程参考价值。; 适合人群:面向具备电力电子、电力系统自动化、新能源并网等专业背景的研究生、高校教师及工程技术人员,特别适用于从事阻抗建模、小干扰稳定性分析、宽频振荡机理研究以及撰写高水平学术论文的科研工作者。; 使用场景及目标:①掌握构网型变流器正负序阻抗建模与扫频辨识的仿真方法;②深入理解VSG等构网型控制在弱电网中提升稳定性的内在机理;③复现顶刊论文中的阻抗分析流程与稳定性判据应用;④利用提供的成熟模型与代码加速科研进程,支撑课题研究与学术成果产出。; 阅读建议:建议结合文中提供的Simulink模型与MATLAB代码,按照“理论建模—仿真搭建—扫频激励—频响提取—Nyquist判据分析”的完整流程进行实践操作,重点关注扫频信号的注入方式、频率范围设置及阻抗曲线的物理意义解读,并参考博士论文复现案例深化对复杂动态耦合问题的理解。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧