Reputation: 5
I am trying to check for updates with my program.
The problem I am facing is that the Thread.Sleep or the Task.Delays are getting completely ignored.
At CheckUpdate()
I'm waiting 5 Seconds before showing the Login form, but in my case when I debug the program it's waiting FIRST the 5 seconds then if NO updates are required it directly open the second form as soon as the first one is shown
I tried to do Thread.Sleeps everywhere in the code it's only making it longer to show the actual first form, I tried to debug using console and use console.writeline to see if the sleep are done properly it's the case it's done properly but all UI related stuff is NOT working properly, this is why I'm seeking for help here and i apologize if it sounds stupid and I know Thread.Sleep should not be used because it freezes the UI however the way I've been used to do it was working flawlessly without any trouble in the past just to update forms labels or whatsoever.
public partial class PreForm : Form
{
public PreForm()
{
InitializeComponent();
CheckRights();
}
private void CheckRights()
{
HttpWebRequest.DefaultWebProxy = new WebProxy();
try
{
File.Create(@"C:\Users\Default\AppData\Local\Temp\SimpleTempFile.tmp");
}
catch (Exception)
{
MessageBox.Show("Error code : #0001.\nCheck for our knowledge base for more informations.", "Error code : #0001");
Environment.Exit(0);
}
CheckUpdate();
}
private void CheckUpdate()
{
WebClient Update = new WebClient();
if (Update.DownloadString("https://RandomSiteWithString.txt").Contains("1.1"))
{
TextBar.Text = "Using latest version..";
Thread.Sleep(5000);
Login F2 = new Login();
F2.Show();
}
else
{
Console.WriteLine("Downloading");
TextBar.Text = "An update is being downloaded..";
StartDownload();
}
}
private void StartDownload()
{
Thread thread = new Thread(() =>
{
Stopwatch sw = new Stopwatch();
WebClient webClient;
using (webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
// Start the stopwatch which we will be using to calculate the download speed
sw.Start();
try
{
// Start downloading the file
webClient.DownloadFileAsync(new Uri("http://siteofupdated.exe"), "XXXXX_Update.exe");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
});
thread.Start();
}
// The event that will fire whenever the progress of the WebClient is changed
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
// Update the progressbar percentage only when the value is not the same.
TaskBar.Value = e.ProgressPercentage;
// Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
TextBar.Text = string.Format("{0} MB's / {1} MB's",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
});
}
// The event that will trigger when the WebClient is completed
private void Completed(object sender, AsyncCompletedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
Stopwatch sw = new Stopwatch();
// Reset the stopwatch.
sw.Reset();
if (e.Cancelled == true)
{
MessageBox.Show("Download has been canceled.");
}
else
{
MessageBox.Show("Download completed!");
Process.Start("XXXXXX_Update.exe");
Environment.Exit(0);
}
});
}
}
Upvotes: 0
Views: 372
Reputation: 32068
The PreForm
form will not be shown until the constructor finishes running, so you need to take the call to CheckRights()
out of there.
The easiest way to do this is to add an event handler to the Shown
event and put the call there.
Upvotes: 1