Otóż program pobiera plik, ale mam dwa problemy. Po 1, nie wiem czemu jak poczekać na inny wątek do zakończenia jednocześnie nie zawieszając sobie gui. Po 2 jak zrobie invoke do labela albo czegokolwiek innego to cały program mi się zawiesza na amen (?). Umyślnie chwilowo wyciąłem zakończenia streamów bo musiałbym robić invoke, a invoke mi wiesza. Nie wiem co robię źle, proszę o pomoc
public void Threadclick()
{
using (WebClient wcDownload = new WebClient())
{
try
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
{
label1.Content = "Downloading File - " + Info.filename;
return null;
}), null); // *** ZWIECHA, bez tego działa, coś robię źle? ***
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(Info.fileglob);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = wcDownload.OpenRead(Info.fileglob);
// Create a new file stream where we will be saving the data (local drive)
string todownload = Info.pathglob2;
if (Info.pathglob2 != "")
todownload += @"\";
todownload += Info.filename;
if (File.Exists(todownload))
File.Delete(todownload);
strLocal = new FileStream(todownload, FileMode.Create, FileAccess.Write, FileShare.None);
// It will store the current number of bytes we retrieved from the server
Int32 bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
PercentProgress = Convert.ToInt32((strLocal.Length * 100) / fileSize);
// this.Dispatcher.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { PercentProgress });
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
// When the above code has ended, close the streams
Info.currentfilecount++;
}
}
}
Wywołanie
Thread thr = new Thread(new ThreadStart(Threadclick));
thr.Start();