Thread af downloads
Hejsa,Jeg har lavet denne funktion:
private void UpdateFiles()
{
string filename = "sources.dat";
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
string line;
while ((line = sr.ReadLine()) != null)
{
string fileList = line;
//MessageBox.Show(check.ToString());
//bool lol = System.Net.HttpWebRequest.Create(fileList);
if (line.Length > 0)
{
statusLabel.Text = "Updating files for " + line + " ...";
try
{
System.Collections.ArrayList result;
result = FetchFileList.ProcessFileFeed(fileList);
progressBar1.Maximum = result.Count;
progressBar1.Value = 0;
int i = 0;
foreach (QuakeUpdater.FileItem currentFileItem in result)
{
currentFileLabel.Text = currentFileItem.File;
string path = distinationTextBox.Text + "\\" + currentFileItem.Dir + "\\" + currentFileItem.File;
string dirpath = distinationTextBox.Text + "\\" + currentFileItem.Dir;
// Check if file already exists, if not then go ahead
if (!System.IO.File.Exists(path))
{
//MessageBox.Show("Download file" + currentFileItem.File + " to dir " + currentFileItem.Dir + " test: " + System.IO.Directory.Exists(currentFileItem.Dir.ToString()));
// Check if directory exists, if not then create
if (!System.IO.Directory.Exists(dirpath))
{
System.IO.Directory.CreateDirectory(dirpath);
MessageBox.Show("Created one folder!");
}
FileDownloader myDownload = new FileDownloader();
// And finally, download the file.
//FileDownloader.DownloadFile(currentFileItem.Dest, path);
myDownload.setPaths(currentFileItem.Dest, path);
t = new Thread(new ThreadStart(myDownload.DownloadFile));
t.Start();
//MessageBox.Show(currentFileItem.Dir);
statusLabel.Text = "Downloaded " + i + " file of " + progressBar1.Maximum + " file(s)";
i++;
}
progressBar1.Value++;
}
if (i > 0)
{
statusLabel.Text = i + " file(s) were downloaded.";
}
else
{
statusLabel.Text = "No files were downloaded.";
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
//progressBar1.Value = 0;
currentFileLabel.Text = "Done.";
}
}
}
sr.Close();
}
Som henter nogle "sources" fra filen sources.dat, disse sources er rss filer som indeholder information omkring hvilke filer brugeren downloader og hvor de skal ligge osv.
Problemet er at programmet 'fryser' mens det downloader - jeg fandt så frem til at jeg kunne bruge threading, DET VIRKER OGSÅ..
Men, nu køre den bare det hele det igennem uden at vente på forrige thread.... dvs den fyre bare alt af og statusbaren/tekts osv bliver skudt af sted på 2 sek mens alle 8 downloads ligger og hygger i baggrunden men dette kan brugeren af programmet jo ikke hvide så han vil bare tro at alt er i orden..
Mit spørgsmål er så:
Hvordan får jeg den til at vente med at gå videre til næste download til den er færdig med downloaded forinden?
- Styrk.
