在datagrindview行中,我有很多网址。
我想从此网址下载thew文件
现在我使用此代码。:
int rowcount = dataGridView1.Rows.Count;
for (int i = 0; i < rowcount; i++)
{
string filename = patch;
WebClient webc = new WebClient();
webc.DownloadFile(dataGridView1.Rows[i].Cells[0].Value.ToString(), patch + "\\" + "alap" + (i+1).ToString() + ".mp4");
}
这对我来说很有用,它一个接一个地下载文件。
我想下载文件paralell(Multithread),例如同时下载5个文件。
答案 0 :(得分:0)
尝试:
int rowcount = dataGridView1.Rows.Count;
List<Task> tasks = new List<Task>()
for (int i = 0; i < rowcount; i++)
{
string filename = patch;
var tsk = Task.Factory.StartNew(() =>
{
try
{
var integerString = (i+1).ToString();
WebClient webc = new WebClient();
webc.DownloadFile(dataGridView1.Rows[i].Cells[0].Value.ToString(), patch + "\\" + "alap" + integeString + ".mp4");
}
catch
{
//log
}
});
tasks.add(tsk);
}
Task.WaitAll(tasks.ToArray());