0

I'am having trouble with my While loop, this while loop, needs to be updates whenever something changed in my While loop the last time.

This is my code, it is running in a thread:

private void CheckAllPorts() { while (true) { MultipleClock = false; OneClock = false; NoClock = false; portCount = 0; //clear the string list. MultiplePortNames.Clear(); //create an object searcher and fill it with the path and the query provided above. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); try { foreach (ManagementObject queryObj in searcher.Get()) { if (queryObj["InstanceName"].ToString().Contains("USB") || queryObj["InstanceName"].ToString().Contains("FTDIBUS")) { portCount = searcher.Get().Count; if (portCount > 1) { MultiplePortNames.Add(queryObj["PortName"].ToString()); form1.UpdateListBox(MultiplePortNames); MultipleClock = true; } else if (portCount == 1) { MultiplePortNames.Add(queryObj["PortName"].ToString()); form1.UpdateListBox(MultiplePortNames); OneClock = true; } } else { NoClock = true; form1.UpdateListBox(MultiplePortNames); } } } catch { NoClock = true; form1.UpdateListBox(MultiplePortNames); } Debug.WriteLine("NoClock = " + NoClock); Debug.WriteLine("OneClock = " + OneClock); Debug.WriteLine("MultipleClock = " + MultipleClock); Thread.Sleep(500); } } 

So if the portCount was 1 last time, and it is this time something else like: 0 or 4, then it needs to execute this code:

form1.UpdateListBox(MultiplePortNames); 

when the portCount was something like 2 last time, and it is also 2 this time, the code shouldn't be executed.

Does anybody know a solution for my problem?

2
  • Why do you have a hard coded "True" in your while statement? Commented Feb 8, 2013 at 8:03
  • That is for debug information only. Commented Feb 8, 2013 at 8:08

3 Answers 3

2

Apart from the obvious problem with the overall structure of this code (can you tell me when you plan to exit from that while(true)?) and focusing only on your question I think you should change the inner loop in this way

 int lastCount = 0; while (true) { portCount = 0; MultipleClock = false; OneClock = false; NoClock = false; //clear the string list. MultiplePortNames.Clear(); //create an object searcher and fill it with the path and the query provided above. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); try { portCount = searcher.Get().Count; foreach (ManagementObject queryObj in searcher.Get()) { if (queryObj["InstanceName"].ToString().Contains("USB") || queryObj["InstanceName"].ToString().Contains("FTDIBUS")) { if (portCount >= 1) MultiplePortNames.Add(queryObj["PortName"].ToString()); } } } catch { // Don't like an empty catch, but perhaps in this case it could be justified } if(portCount == 1) OneClock = true; else if(portCount > 1) MultipleClock = true; else NoClock = true; if(lastCount != portCount) { lastCount = portCount; form1.UpdateListBox(MultiplePortNames); } Debug.WriteLine("NoClock = " + NoClock); Debug.WriteLine("OneClock = " + OneClock); Debug.WriteLine("MultipleClock = " + MultipleClock); Thread.Sleep(500); } 

I have added a lastCount variable to keep track of the result of the previous loop over the USB port discovery code and changed the inner loop to call the listbox update only at the end of the foreach loop. Don't know if the xxxClock variables are still of use or not.

Sign up to request clarification or add additional context in comments.

6 Comments

It is running in a thread, and this thread will never stop, this thread will show all connected usb devices in a listbox, so when one is plugged out, it will be visible immediatly,
Very bad idea to call Count() method agains Enumerable in body of foreach
Which way should you recommend, to count the number of results from the query by searcher.Get() ?
@YuriyRozhovetskiy: It is not necessarily performant, but it works - but then, the OP isn't calling the Count() extension method for IEnumerable<T> there; it looks like a property access to Count, so I assume searcher.Get() actually returns a List<T>.
@YuriyRozhovetskiy, right I haven't catched that. Could be easily scoped out
|
1

You need to add another variable where you keep the value of the previous check. Then you can compare the current amount with the previous, and do your logic accordingly.

Store that value as the last step in your try-block. Do NOT place the int newVar=0 at the start of your while-block though, or this will have not the required result.

You also need to clean up your code a bit.

Edit: Looks like Steve just did it. (The extra var+clean up)

Comments

1

Try to rearrange your code like this:

private void CheckAllPorts() { while (true) { MultipleClock = false; OneClock = false; NoClock = false; portCount = 0; //clear the string list. MultiplePortNames.Clear(); //create an object searcher and fill it with the path and the query provided above. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); try { var results = searcher.Get().Where(queryObj=> queryObj["InstanceName"].ToString().Contains("USB") || queryObj["InstanceName"].ToString().Contains("FTDIBUS")); if (portCount != results.Count()) { portCount = results.Count(); if (portCount > 1) { MultipleClock = true; } else if (portCount == 1) { OneClock = true; } else if (portCount == 0) { NoClock = true; } foreach (ManagementObject queryObj in results) { MultiplePortNames.Add(queryObj["PortName"].ToString()); } form1.UpdateListBox(MultiplePortNames); } } catch { NoClock = true; form1.UpdateListBox(MultiplePortNames); } Debug.WriteLine("NoClock = " + NoClock); Debug.WriteLine("OneClock = " + OneClock); Debug.WriteLine("MultipleClock = " + MultipleClock); Thread.Sleep(500); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.