C# 循环时,检查上次运行
我的While循环有问题,这个While循环需要在上次While循环中发生更改时进行更新 这是我的代码,它在线程中运行:C# 循环时,检查上次运行,c#,.net,foreach,while-loop,C#,.net,Foreach,While Loop,我的While循环有问题,这个While循环需要在上次While循环中发生更改时进行更新 这是我的代码,它在线程中运行: private void CheckAllPorts() { while (true) { MultipleClock = false; OneClock = false; NoClock = false; portCount = 0;
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);
}
}
因此,如果上次端口计数是1,而这次是其他类似的值:0或4,那么
它需要执行以下代码:
form1.UpdateListBox(MultiplePortNames);
当上次端口计数为2时,而这次也是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循环结束时调用listbox update。不知道xxxClock变量是否仍然在使用。除了代码整体结构的明显问题(你能告诉我你打算什么时候退出,而不是(真的)退出)和只关注你的问题之外,我认为你应该以这种方式改变内部循环
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循环结束时调用listbox update。不知道xxxClock变量是否仍在使用。您需要在保留上一次检查值的位置添加另一个变量。
然后,您可以将当前金额与之前的金额进行比较,并相应地执行您的逻辑
将该值存储为try块中的最后一步。
但是,不要将int-newVar=0放在while块的开头,否则将不会得到所需的结果
您还需要稍微清理一下代码
编辑:看来是史蒂夫干的。(额外的var+清理)您需要添加另一个变量,在其中保留上一次检查的值。 然后,您可以将当前金额与之前的金额进行比较,并相应地执行您的逻辑 将该值存储为try块中的最后一步。 但是,不要将int-newVar=0放在while块的开头,否则将不会得到所需的结果 您还需要稍微清理一下代码
编辑:看来是史蒂夫干的。(额外的var+清理)尝试如下重新排列代码:
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);
}
}
尝试按如下方式重新排列代码:
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);
}
}
为什么在while语句中有一个硬编码的“True”?这只是为了调试信息。为什么在while语句中有一个硬编码的“True”?这只是为了调试信息。它在一个线程中运行,并且这个线程永远不会停止,这个线程将在一个列表框中显示所有连接的usb设备,所以当一个被拔出时,如果在
foreach
的主体中再次调用Count()
方法Enumerable,那么您应该推荐哪种方法来计算搜索者查询的结果数。Get()?@YuriyRozhovetskiy:这不一定是性能,但它是有效的-但是,OP没有调用Count()
IEnumerable的扩展方法here;它看起来像是对Count
的属性访问,所以我假设searcher.Get()
实际上返回了一个列表
@YuriyRozhovetskiy,对,我还没有捕捉到。它在线程中运行,并且该线程永远不会停止,该线程将在列表框中显示所有连接的usb设备,因此当一个设备被插入时,它将立即可见,在foreach
的主体中再次调用Count()
方法Enumerable是一个非常糟糕的主意,您应该推荐哪种方法,要通过searcher.Get()计算查询结果的数量?@YuriyRozhovetskiy:这不一定是性能问题,但它是有效的-但是,OP没有在那里调用IEnumerable
的count()
扩展方法;它看起来像是对Count
的属性访问,所以我假设searcher.Get()
实际上返回了一个列表
@YuriyRozhovetskiy,对,我还没有捕捉到。可以很容易地确定范围