Solved: Flickering when updating GroupBox

GroupBox flickers when i update it from another thread

Page 1 of 1

4 Replies - 3386 Views - Last Post: 20 March 2009 - 04:37 PM Rate Topic: -----

#1 Parre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 09

Solved: Flickering when updating GroupBox

Posted 20 March 2009 - 03:33 PM

I am trying to set enabled=true or false on a GroupBox depending on wether a removable drive is present or not with this code:

private void findUsbStick()
		{
			while (true)
			{
				DriveInfo[] driveInfoList = DriveInfo.GetDrives();
				foreach (DriveInfo driveFound in driveInfoList)
				{
					if (driveFound.Name == drive)
					{
						groupBox2.Invoke((MethodInvoker)delegate { groupBox2.Enabled = true; });
					}
					else
					{
						groupBox2.Invoke((MethodInvoker)delegate { groupBox2.Enabled = false; });
					}
				}
			}
		}


The problem is that the GroupBox is flickering and is non responsive for a short period when it gets updated.
It seems like the foreach both enables and disables the GroupBox everytime it loops through.

I've searched around and found a few solutions including DoubleBuffer but i'm not sure that's the problem. I think the problem lies in the loop, am i right?

As you probably can tell i'm pretty new to all this (both C# and threading) and i was hoping someone could help me get this thing sorted and maybe tell me how to make it more efficient.

Regards
Parre

This post has been edited by Parre: 20 March 2009 - 04:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Solved: Flickering when updating GroupBox

#2 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Solved: Flickering when updating GroupBox

Posted 20 March 2009 - 04:04 PM

Well, first off, to do something as simple as disabling/enabling your groupbox, you don't need to use the invoke method. Just simply say "groupbox2.Enabled = true", or false. Next, get rid of that "While (true)" loop. I see no need for that, and that will do nothing but make your app unresponsive. Now, your groupbox is changing it's enabled state every time it runs through the loop, which is what you don't want. I'm not completely sure what you are wanting to do, but based off that code and on your description, i am assuming you want to enable your groupbox if a drive is found with a name that matches "drive". So, i think if, just before you start your foreach loop, you can put "groupbox2.Enabled = false" (to make sure it's disabled before you start), then, change
                    if (driveFound.Name == drive)
                    {
                        groupBox2.Invoke((MethodInvoker)delegate { groupBox2.Enabled = true; });
                    }
                    else
                    {
                        groupBox2.Invoke((MethodInvoker)delegate { groupBox2.Enabled = false; });
                    }


to
					if (driveFound.Name == drive)
					{
						groupBox2.Enabled = true;
						break;
					}


As you can see, i've gotten rid of the else clause since the groupbox is already disabled, and added a break so the loop will stop running through once the drive is found. I also removed the invoke method thing as i said. This may not be what you are looking for, but it's my best guess as to what you are wanting to do.

This post has been edited by jacobjordan: 20 March 2009 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 Parre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 09

Re: Solved: Flickering when updating GroupBox

Posted 20 March 2009 - 04:15 PM

Thank you for the reply!

I use the while loop to keep checking wheter the usb-drive is still inserted since i need to disable the GroupBox if the usb-drive is not present. It will not make my application unresponsive since it's running in it's own thread.

I'm pretty sure i need the Invoke to change the enabled state of the GroupBox from another thread.

I'm sorry if i was a bit unclear in my post.
Was This Post Helpful? 0
  • +
  • -

#4 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Solved: Flickering when updating GroupBox

Posted 20 March 2009 - 04:24 PM

I was not aware this code was running on another thread, so yes, you are right that you need to use invoke. You can also get away with using a while loop to check if the drive was removed, however one change i would make so that you app doesn't keep using up the computer's CPU, is to add a "System.Threading.Thread.Sleep(500)" at the beginning of your while loop. That will make the thread "Sleep", or not do anything, for 1/2 second, that way your app will be more CPU friendly. In other words, you app will check to see if the drive was removed every 1/2 second. Other than that, the other changes i suggested will work (i think).

Edit:, Oh also, instead of using
                    if (driveFound.Name == drive)
                    {
                        groupBox2.Enabled = true;
                        break;
                    }


and putting the "Enabled=false" just before your for loop, you will need to use
                bool foundDrive = false;
                foreach (DriveInfo driveFound in driveInfoList)
                {
                    if (driveFound.Name == drive)
                    {
                        foundDrive = true;
                    }
                }
                groupBox2.Invoke((MethodInvoker)delegate { groupBox2.Enabled = foundDrive; });


And remove the line to disable the group box just before the for loop.
That way, your group box won't keep enabling/disabling.

This post has been edited by jacobjordan: 20 March 2009 - 04:31 PM

Was This Post Helpful? 1
  • +
  • -

#5 Parre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-March 09

Re: Solved: Flickering when updating GroupBox

Posted 20 March 2009 - 04:37 PM

Works perfectly! Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1