Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:54:09+00:00 2026-06-18T14:54:09+00:00

I’am having trouble with my While loop, this while loop, needs to be updates

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:54:10+00:00Added an answer on June 18, 2026 at 2:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.