Timer control freezing the UI
-
Hello, CF 3.5 VS 2008 I have a timer control that is being used to do some work. Code below. The timer interval is set for 1 second. However, when the timer is running the UI freezes for a short period. I understand that the timer control is uses the UI thread, but I didn't think the amount of work was too much to make the UI freeze for short periods. I couldn't see any server timers for the compact framework. Any suggestions would be most helpful.
//Get the signal strength of the AP that the adapter is currently connected to private void GetSignalStrength() { try { //Loop through all the AP nearby and find the one that is currently being connected (Associated) if (currentAdapter.NearbyAccessPoints.Count > 0) { foreach (AccessPoint ap in currentAdapter.NearbyAccessPoints) { if (ap.Name == currentAdapter.AssociatedAccessPoint) { this.DisplaySignalStrength(ap.SignalStrength.ToString()); //Break out of for loop when the currently connected AP has been found. break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //Display the signal strength by filling the bars depending on //the strength of the access point. private void DisplaySignalStrength(string strength) { this.pbExcellent.BackColor = Color.Transparent; this.pbVeryGood.BackColor = Color.Transparent; this.pbGood.BackColor = Color.Transparent; this.pbLow.BackColor = Color.Transparent; this.pbVeryLow.BackColor = Color.Transparent; switch (strength) { case "Excellent": this.pbExcellent.BackColor = Color.Green; this.pbVeryGood.BackColor = Color.Green; this.pbGood.BackColor = Color.Green; this.pbLow.BackColor = Color.Green; this.pbVeryLow.BackColor = Color.Green; break; case "Very Good": this.pbVeryGood.BackColor = Color.Green; this.pbGood.BackColor = Color.Green;
-
Hello, CF 3.5 VS 2008 I have a timer control that is being used to do some work. Code below. The timer interval is set for 1 second. However, when the timer is running the UI freezes for a short period. I understand that the timer control is uses the UI thread, but I didn't think the amount of work was too much to make the UI freeze for short periods. I couldn't see any server timers for the compact framework. Any suggestions would be most helpful.
//Get the signal strength of the AP that the adapter is currently connected to private void GetSignalStrength() { try { //Loop through all the AP nearby and find the one that is currently being connected (Associated) if (currentAdapter.NearbyAccessPoints.Count > 0) { foreach (AccessPoint ap in currentAdapter.NearbyAccessPoints) { if (ap.Name == currentAdapter.AssociatedAccessPoint) { this.DisplaySignalStrength(ap.SignalStrength.ToString()); //Break out of for loop when the currently connected AP has been found. break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //Display the signal strength by filling the bars depending on //the strength of the access point. private void DisplaySignalStrength(string strength) { this.pbExcellent.BackColor = Color.Transparent; this.pbVeryGood.BackColor = Color.Transparent; this.pbGood.BackColor = Color.Transparent; this.pbLow.BackColor = Color.Transparent; this.pbVeryLow.BackColor = Color.Transparent; switch (strength) { case "Excellent": this.pbExcellent.BackColor = Color.Green; this.pbVeryGood.BackColor = Color.Green; this.pbGood.BackColor = Color.Green; this.pbLow.BackColor = Color.Green; this.pbVeryLow.BackColor = Color.Green; break; case "Very Good": this.pbVeryGood.BackColor = Color.Green; this.pbGood.BackColor = Color.Green;
-
Indeed. Start a new thread to run the timer on and when the OnTick event is triggered, I believe you can use this.Involk(); to involk a delegate method to be handled in the main thread? Not sure but, its a suggestion. Andy
-
Indeed. Start a new thread to run the timer on and when the OnTick event is triggered, I believe you can use this.Involk(); to involk a delegate method to be handled in the main thread? Not sure but, its a suggestion. Andy
Hello, I didn't really want to use threading. As I would have to invoke all the picture boxes to be updated. As the picture boxes are created on the UI thread. However, I am experimenting with the code below. Any suggestions would be most helpfull. Thanks,
Thread th; public void StartPolling() { this.GetWirelessAdapters();//Call only once to setup the adapters. th = new Thread(ThreadPollStrength); th.IsBackground = true; th.Start(); } private void ThreadPollStrength() { this.GetSignalStrength();//Will continue to call every one second. Thread.Sleep(1000); }
-
Hello, I didn't really want to use threading. As I would have to invoke all the picture boxes to be updated. As the picture boxes are created on the UI thread. However, I am experimenting with the code below. Any suggestions would be most helpfull. Thanks,
Thread th; public void StartPolling() { this.GetWirelessAdapters();//Call only once to setup the adapters. th = new Thread(ThreadPollStrength); th.IsBackground = true; th.Start(); } private void ThreadPollStrength() { this.GetSignalStrength();//Will continue to call every one second. Thread.Sleep(1000); }
Just read how invoking control in UI because the control an a UI runs in separated thread
delegate void UpdatePictureBoxCallBack(PictureBox pb, Color color);
private void UpdatePictureBox(PictureBox pb, Color color)
{
if (pb.InvokeRequired){
pb.Invoke(new UpdatePictureBoxCallBack(UpdatePictureBox), new object[] {pb, color});
}else{
pb.BackgroundColor=color;
}
}call this method for each condition and picture box on your timer ticks event as you submit early i hope this usefull
dhaim program is hobby that make some money as side effect :)