[C#] Mon Thread ferme mon application
Bonjour je suis un débutant en programmation et je suis sur un projet qui a pour but de télécharger un fichier en ligne avec la fonction downloadFile() mais j'ai un problème assez gênant c'est que quand je lance un thread avec ma fonction mon application ce ferme mais le fichier et bien télécharger.
je lance mon Thread comme ceci :
Code:
1 2 3
|
Thread th = new Thread(this.Update);
th.Start(); |
et voici ma fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
public void Update()
{
cf.xmlStream("config.xml");
string pathVersion = cf.xmlReadNodeChild("pathversion");
string pathDownload = cf.xmlReadNodeChild("pathdownload");
//check online
version = _WebClient.DownloadString(pathVersion);
//xml
string xmlVersion = cf.xmlReadNodeChild("version");
//Checked version
if (version == xmlVersion)
{
//on met le statut a jour
label1.Content = "Votre Jeu et a jours";
button2.IsEnabled = true;
//MessageBox.Show("done");//un test pour voir si la condition est bien respecté
}
else if (version != xmlVersion)
{
//on telecharge
_WebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
_WebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Progress);
_WebClient.DownloadFileAsync(new Uri(pathDownload), "" + core.pathDownload() + @"\" + "config1111.xml");
label1.Content = "Téléchargement en cours";
}
else
{
MessageBox.Show("Erreur le fichier n'as pas pu etre télécharger peut etre est il inexistant");
}
//
}
public void Completed(object sender, AsyncCompletedEventArgs e)
{
cf.xmlStream("config.xml");
cf.xmlChangeValueNode("version", version);
label1.Content = "Votre Jeu et a jours";
button2.IsEnabled = true;
}
public void Progress(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
} |
Alors voila, si vous pourriez me venir en aide je vous en serais très reconnaissant.
Merci.