Reputation: 85
I'm trying to make a "file-updater" that when the program opens, detects if exists a folder called "mods", the corresponding files and their sizes (in bytes). Actually it works! But i have a label that informs what file are downloading and it's only showing the last message "Actualizacion finalizada.Ya puedes jugar!" instead of "Descargando " + ArmorSts", "Descargando " + TreeCpt" and blablabla. ,how can i inform with the label what file i'm downloading right now and what file are updated? if someone need to see the messages in english, let me know.
string ArmorSts = "[1.7.10]ArmorStatusHUD-client-1.28.jar";
string TreeCpt = "[1.7.10]Treecapitator-universal-2.0.4.jar";
string AdvSolar = "AdvancedSolarPanel-1.7.10-3.5.1.jar";
private void Form1_Load(object sender, EventArgs e)
{
string path = @"C:\Users\" + System.Environment.UserName + "\\AppData\\Roaming\\.minecraft\\mods\\";
if (!Directory.Exists(path)) /* Si no existe, entonces... */
{
MessageBox.Show("La carpeta .minecraft (" + path + ") no se encuentra. Por favor instale minecraft.",
"Error al encontrar el directorio",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
/* Busquemos el directorio entonces... */
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
/* si cancelamos entonces cerramos el programa */
else
{
Application.Exit();
}
}
/* Si la carpeta existe, entonces... */
else {
/* Si el mod esta, entonces... */
FileInfo mod_1 = new FileInfo(path + ArmorSts);
/* Checkeamos si pesa igual */
if (File.Exists(path + ArmorSts) && mod_1.Length == 27281)
{
label1.Text = ArmorSts + " - Actualizado";
goto Siguiente_1;
}
else
{
/* Si el mod no esta o pesa diferente, entonces */
label1.Text = "Descargando " + ArmorSts;
/* Descargar en %appdata%/.minecraft/mods */
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + ArmorSts), path + ArmorSts);
goto Siguiente_1;
}
Siguiente_1 :
FileInfo mod_2 = new FileInfo(path + TreeCpt);
if (File.Exists(path + TreeCpt) && mod_2.Length == 94792)
{
label1.Text = TreeCpt + " - Actualizado";
goto Siguiente_2;
}
else
{
label1.Text = "Descargando " + TreeCpt;
/* Descargar en %appdata%/.minecraft/mods */
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + TreeCpt), path + TreeCpt);
goto Siguiente_2;
}
Siguiente_2:
FileInfo mod_3 = new FileInfo(path + AdvSolar);
if (File.Exists(path + AdvSolar) && mod_3.Length == 305645)
{
label1.Text = AdvSolar + " - Actualizado";
goto Siguiente_3;
}
else
{
label1.Text = "Descargando " + AdvSolar;
/* Descargar en %appdata%/.minecraft/mods */
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + AdvSolar), path + AdvSolar);
goto Siguiente_3;
}
/*
...
... and on and on and on ...
...
*/
Siguiente_23:
FileInfo mod_24 = new FileInfo(path + Witchery);
if (File.Exists(path + Witchery) && mod_24.Length == 7009956)
{
label1.Text = Witchery + " - Actualizado";
Thread.Sleep(1000);
ActualizacionFinalizada();
}
else
{
label1.Text = "Descargando " + Witchery;
/* Descargar en %appdata%/.minecraft/mods */
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + Witchery), path + Witchery);
ActualizacionFinalizada();
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
colorProgressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
Actualizando();
label1.Text = "Actualizado";
}
private void ActualizacionFinalizada()
{
Actualizando();
label1.Text = "Actualizacion finalizada. Ya puedes jugar!";
}
I'm learning C# independently and i know this is not the correct way (repeating the same code with little modifications) if there's a best way in simplifying this code or with best performance i will be really grateful!
Upvotes: 0
Views: 915
Reputation: 415690
This cuts to the basic principle of WinForms. There is one thread for updating the user interface. When you set properties on controls that change their appearance, it causes a Paint
event to fire on that thread. Your changes won't be visible until you return program control back to that thread.
What you want to do here to fix your problem is move most of your Form_Load code to a BackgroundWorker control. This control exists explicitly to provide a place for long-running methods that would otherwise block the user interface thread.
Also: goto
? Really?
Upvotes: 1