While循环,检查上次运行

本文关键字:运行 检查 循环 While | 更新日期: 2023-09-27 17:58:31

我的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时,不应该执行代码。

有人知道解决我问题的办法吗?

While循环,检查上次运行

除了这段代码的整体结构存在明显问题之外(你能告诉我你计划何时退出while(true)吗?)只关注你的问题我认为你应该以这种方式改变内部循环

    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更新。不知道xxxClock变量是否仍在使用。

您需要添加另一个变量来保存上一次检查的值。然后,您可以将当前金额与以前的金额进行比较,并相应地进行逻辑运算。

将该值存储为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);
    }
}