Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timer control freezing the UI

Timer control freezing the UI

Scheduled Pinned Locked Moved C#
visual-studiodesignsysadmin
5 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_rm
    wrote on last edited by
    #1

    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;

    A 1 Reply Last reply
    0
    • S steve_rm

      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;

      A Offline
      A Offline
      AB7771
      wrote on last edited by
      #2

      Use threading...

      Thanks & Regards, Pramod "Everyone is a genius at least once a year"

      A 1 Reply Last reply
      0
      • A AB7771

        Use threading...

        Thanks & Regards, Pramod "Everyone is a genius at least once a year"

        A Offline
        A Offline
        Abydosgater
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • A Abydosgater

          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

          S Offline
          S Offline
          steve_rm
          wrote on last edited by
          #4

          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); }

          M 1 Reply Last reply
          0
          • S steve_rm

            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); }

            M Offline
            M Offline
            Mbah Dhaim
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups