我遇到了我的While循环问题,这个while循环需要在我的While循环中最后一次更改时更新。
这是我的代码,它在一个帖子中运行:
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);
}
}
所以如果portCount最后一次是1,那么这次是其他的东西,比如:0或4,那么 它需要执行此代码:
form1.UpdateListBox(MultiplePortNames);
当portCount最后一次为2时,这次也是2,不应该执行代码。
有人知道我的问题的解决方案吗?
答案 0 :(得分:2)
除了这个代码整体结构的明显问题(你可以告诉我你什么时候退出那个(真)?)并只关注你的问题我认为你应该用这种方式改变内循环
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);
}
我添加了一个lastCount
变量来跟踪USB端口发现代码上一个循环的结果,并更改了内部循环以仅在foreach循环结束时调用列表框更新。不知道xxxClock变量是否仍在使用。
答案 1 :(得分:1)
您需要添加另一个变量,以保留上一次检查的值。 然后,您可以将当前金额与之前的金额进行比较,并相应地做出相应的逻辑。
将该值存储为try-block的最后一步。 不要在你的while-block的开头放置int newVar = 0,否则这将没有所需的结果。
您还需要稍微清理一下代码。
编辑:看起来史蒂夫刚刚做到了。 (额外的var +清理)
答案 2 :(得分:1)
尝试重新排列代码,如下所示:
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);
}
}