0

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

有人知道我的问题的解决方案吗?

4

3 回答 3

2

除了这段代码的整体结构存在明显问题(你能告诉我你打算什么时候退出那个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 循环结束时调用列表框更新。不知道 xxxClock 变量是否仍在使用。

于 2013-02-08T08:11:22.577 回答
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);
    }
}
于 2013-02-08T08:16:39.983 回答
1

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

将该值存储为 try 块中的最后一步。但是,不要将 int newVar=0 放在 while 块的开头,否则这将没有所需的结果。

您还需要稍微清理一下代码。

编辑:看起来史蒂夫刚刚做到了。(额外的变量+清理)

于 2013-02-08T08:11:37.630 回答