I have a problem while trying to download a file less than 1mb divide into two parts of byte[]. It throws System.ArgumentException at progress % 49 and at Buffer.BlockCopy line in Download(1) method.I cant understand what is the reason?
public void Download1()
{
req1 = WebRequest.Create(url) as HttpWebRequest;
req1.ServicePoint.ConnectionLimit = 20;
req1.AddRange(sp[0], fp[0] - 1);
WebResponse resp1 = req1.GetResponse();
Stream str1 = resp1.GetResponseStream();
int readBytes = 0;
int stp = 0;
byte[] buffer = new byte[1024];
while ((readBytes = str1.Read(buffer, 0, buffer.Length)) > 0)
{
Buffer.BlockCopy(buffer, 0, data1, stp, readBytes);
stp += readBytes;
totalbayt1 = stp;
UpdateProgressBar();
}
}
HttpWebRequest req2;
int totalbayt1 = 0, totalbayt2 = 0;
public void Download2()
{
req2 = WebRequest.Create(url) as HttpWebRequest;
req2.ServicePoint.ConnectionLimit = 20;
req2.AddRange(sp[1], filesize);
WebResponse resp2 = req2.GetResponse();
Stream str2 = resp2.GetResponseStream();
int readBytes = 0;
int stp = 0;
byte[] buffer = new byte[1024];
while ((readBytes = str2.Read(buffer, 0, buffer.Length)) > 0)
{
Buffer.BlockCopy(buffer, 0, data2, stp, readBytes);
stp += readBytes;
totalbayt2 = stp;
UpdateProgressBar();
}
}
public void DownloadFile()
{
t1 = new Task(Download1);
t2 = new Task(Download2);
sp[0] = 0;
fp[0] = (int)(filesize / 2);
sp[1] = fp[0];
fp[1] = (int)(filesize - sp[1]);
data2 = new byte[fp[0]];
data1 = new byte[fp[1] + 1];
t1.Start();
t2.Start();
st.Start();
Task.WaitAll(t1, t2);
writeToFile(path);
}