I've been through and through the search results of this website but cant seem to find what im looking for, And the only close thread that I found never got answered. Im trying to prevent my UI from freezing when downloading files but Id like everything to work on Form Load instead of a button click or something else. Heres the code Im running with right now:
private void Form1_Load(object sender, EventArgs e)
{
this.Show();
if (!IsConnectedToInternet) //user is not connected to internet
{
MessageBox.Show("Not connected to internet!\n Please check your connection.", settings.cheatname);
}
else //user is connected to internet
{
WebClient client = new WebClient();
System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
var newversion = client.DownloadString(settings.versiontxt);
var newversionparsed = int.Parse(newversion);
if (settings.version < newversionparsed)
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
Stream[] sounds = new Stream[] {
_2B.Properties.Resources.russian,
_2B.Properties.Resources.byebye
};
Stream sound = sounds[random.Next(sounds.Length)];
SoundPlayer audio = new SoundPlayer(sound);
audio.Play();
//System.Threading.Thread.Sleep(2300); //<=== Testing wait theory.
// Downloading the new version
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(settings.loaderexe, Directory.GetCurrentDirectory() + "/" + finalString + ".exe");
System.Diagnostics.Process.Start(Directory.GetCurrentDirectory() + "/" + finalString + ".exe");
Application.Exit();
string batchCommands = string.Empty;
string exeFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty).Replace("/", "\\");
}
else
{
Login nextForm = new Login();
this.Hide();
nextForm.ShowDialog();
this.Close();
}
}
}
}
}
Now I know I have to use a backgroundworker but I cant seem to get it to work im not very smart when it comes to splitting threads and stuff so the usage of a backgroundworker to me is completely new. I'd like to learn but I wanna know how it SHOULD be so I can note that for future reference. Any help would be appreciated and posted code examples on how to fix or make what I need to do possible would be great.