Results 1 to 4 of 4

Thread: Timer control freezing the UI

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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.

    Code:
    //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;
                        this.pbLow.BackColor = Color.Green;
                        this.pbVeryLow.BackColor = Color.Green;
                        break;
                    case "Good":
                        this.pbGood.BackColor = Color.Green;
                        this.pbLow.BackColor = Color.Green;
                        this.pbVeryLow.BackColor = Color.Green;
                        break;
                    case "Low":
                        this.pbLow.BackColor = Color.Green;
                        this.pbVeryLow.BackColor = Color.Green;
                        break;
                    case "Very Low":
                        this.pbVeryLow.BackColor = Color.Green;
                        break;
                    default:
                        break;
                }
            }
    
            //Check the signal strength every 1 second
            private void tmrPollAP_Tick(object sender, EventArgs e)
            {
                this.GetSignalStrength();   
            }
    Last edited by steve_rm; Jun 25th, 2008 at 09:40 PM.
    steve

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Timer control freezing the UI

    Actually, I would suspect that the amount of time being taken by what you are doing may well be longer than you think. To me, checking signal strength seems like a time-intensive operation. You might write yourself a little profiler to test it. I have one in VB, but they are so easy to write that it's hardly worth passing it out. Just start a stopwatch object at the beginning of the tick event, stop it at the end, and look at the elapsed time. I think you will probably find that the timing is not what you had hoped for. If that is the case, I would say that you should seriously consider checking the signal strength in a background thread. This could either run periodically (you could start it on the timer tick), or run continuously (which might impact performance, too), only updating the display periodically.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Timer control freezing the UI

    Hello,

    Visual Studio 2008 development edition comes with a profiler that measures the time it takes. I will investigate this.

    I was thinking of using a background thread. However, as the picture boxes are on the UI thread then I would have to use the picture boxes to invoke a delegate to marshal the call to the UI thread. Just seems a lot of coding for something that seemed easy.

    However, I will try the background thread and post back with the code.

    Thanks,
    steve

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Timer control freezing the UI

    I would do the profiling first. I suspect the signal strength takes a certain amount of time, but I'd want to confirm that it is the cause of the slowdown.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width