小弟有一事请教大家,最近在调试一个支持HTTP和FTP多线程下载工具,其中使用到processbar来显示下载进度,相同的算法HTTP下载时processbar.value值没有超出最大值(最大值为所要下载文件的大小),而FTP却下载到2/3的时候就出错并提示processbar.value超出最大值,我想当的郁闷,实在不知道问题出在哪里,浪费大家宝贵的时间帮帮小弟,谢谢大家!下面为相关代码:
public class InforClass
{
private static int threadcount;//线程数量
public static int[] threadPercent;//线程下载文件完成量
public static int[] threadisComplate;//线程下载文件是否完成
public static string msg = "下载文件开始";
public static int FileSize;//下载文件的大小;
public static DateTime startTime;//开始下载时间
public static int ThreadCount
{
get
{
return threadcount;
}
set
{
threadcount = value;
threadPercent = new int[threadcount];
for (int i = 0; i < threadcount; i++)
{
threadPercent[i] = 0;
}
threadisComplate = new int[threadcount];
for (int i = 0; i < threadcount; i++)
{
threadisComplate[i] = 0;
}
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (ftpfile != null)
{
int data = 0;
for (int i = 0; i < InforClass.ThreadCount; i++)
{
data += InforClass.threadPercent[i];
}
progressBar1.Maximum = InforClass.FileSize;
progressBar1.Value = data;
label5.Text = InforClass.msg;
TimeSpan AllTs = DateTime.Now - InforClass.startTime;//已下载文件的时间
double PByte = data / AllTs.TotalSeconds / 1024;//计算速度,byte/秒换算成KB/秒
if (InforClass.msg.IndexOf("正在下载") > 0)
{
label6.Text = string.Format("{0}KB/秒", (int)PByte);
}
else
{
label6.Text = "0KB/秒";
} }
}
public class InforClass
{
private static int threadcount;//线程数量
public static int[] threadPercent;//线程下载文件完成量
public static int[] threadisComplate;//线程下载文件是否完成
public static string msg = "下载文件开始";
public static int FileSize;//下载文件的大小;
public static DateTime startTime;//开始下载时间
public static int ThreadCount
{
get
{
return threadcount;
}
set
{
threadcount = value;
threadPercent = new int[threadcount];
for (int i = 0; i < threadcount; i++)
{
threadPercent[i] = 0;
}
threadisComplate = new int[threadcount];
for (int i = 0; i < threadcount; i++)
{
threadisComplate[i] = 0;
}
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (ftpfile != null)
{
int data = 0;
for (int i = 0; i < InforClass.ThreadCount; i++)
{
data += InforClass.threadPercent[i];
}
progressBar1.Maximum = InforClass.FileSize;
progressBar1.Value = data;
label5.Text = InforClass.msg;
TimeSpan AllTs = DateTime.Now - InforClass.startTime;//已下载文件的时间
double PByte = data / AllTs.TotalSeconds / 1024;//计算速度,byte/秒换算成KB/秒
if (InforClass.msg.IndexOf("正在下载") > 0)
{
label6.Text = string.Format("{0}KB/秒", (int)PByte);
}
else
{
label6.Text = "0KB/秒";
} }
}
progressBar1.Value
均是int型,超过2G就溢出。给processBar传值时单位缩小一下吧,例如都除以1025