Answer the question
In order to leave comments, you need to log in
How to properly organize multi-threaded/asynchronous downloads?
I download via WebClient. How will it be more correct and faster:
1 way:
Task.Factory.StartNew(() =>
{
myload(max_id);
}).ContinueWith((t) => {
result.Text = "saved!";
result.ForeColor = System.Drawing.Color.Green;
}, TaskScheduler.FromCurrentSynchronizationContext());
private void myload(int max_id)
{
int i = 1;
while(i <= max_id)
{
filename = path_tosave + "/" + i + ".png";
loadurl = lurl + i + ".png";
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += download_completed;
webClient.DownloadFileAsync(new Uri(loadurl), filename);
i++;
}
}
Answer the question
In order to leave comments, you need to log in
you break a lot of indices into parts, for example 1-100, 100-200, 200-300
in a cycle of 3 tasks you run
var tasks = new List<Task>();
for(var i=0;i<3;i++){
tasks.Add(Task.Run(()=>{
myload(from, till); // метод изменить, чтобы он индексы от и до принимал
}));
}
Task.WaitAll(tasks.ToArray());
// тут кусок кода после завершения всех
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question