Reputation: 13388
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?
Upvotes: 0
Views: 289
Reputation: 32571
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);
}
}
Upvotes: 1
Reputation: 216353
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.
Upvotes: 2
Reputation: 21
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)
Upvotes: 1