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

  • Home
  • SEARCH
  • 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 3499884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:39:58+00:00 2026-05-18T12:39:58+00:00

I’m attempting to write a function which starts a thread for each contact and

  • 0

I’m attempting to write a function which starts a thread for each “contact” and then queries (over the network) for results from that contact. I want my waiting function to wait at most 1.5 second for responses, and after that, simply terminate any remaining threads.

The issue I’m having is that the function is returning before all the threads are done, even though according to the logic, this should not be possible. The while loop in the main function should be waiting until all threads have fully completed, yet I get the following output:

FAIL: Storage test 1 exists 0 times in the DHT.
    : Storage test 2 exists 0 times in the DHT.
Added storage test 1 to the entries.
Added storage test 2 to the entries.

(The FAIL lines comes from the main testing program seeing how many results were returned by Get().)

According to what I can see, this shouldn’t be possible. Does anyone know where the race condition might be occurring (or any other assumptions I’ve made that are not correct)?

The function definition is as such:

    public IList<Entry> Get(ID key)
    {
        ConcurrentBag<Entry> entries = new ConcurrentBag<Entry>();
        List<Thread> threads = new List<Thread>();
        foreach (Contact c in this.p_Contacts)
        {
            Thread t = new Thread(delegate()
            {
                try
                {
                    FetchMessage fm = new FetchMessage(this, c, key);
                    fm.Send();
                    int ticks = 0;

                    // Wait until we receive data, or timeout.
                    while (!fm.Received && ticks < 1500)
                    {
                        Thread.Sleep(100);
                        ticks += 100;
                    }

                    if (fm.Received)
                    {
                        foreach (Entry e in fm.Values)
                        {
                            Console.WriteLine("Added " + e.Value + " to the entries.");
                            entries.Add(e);
                        }

                        if (entries.Count == 0)
                            Console.WriteLine("There were no entries to add.");
                    }
                    else
                        Console.WriteLine("The node did not return in time.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            );
            t.IsBackground = false;
            t.Start();
        }

        while (true)
        {
            bool stopped = true;
            foreach (Thread t in threads)
            {
                if (t.ThreadState != ThreadState.Stopped)
                    stopped = false;
            }
            if (stopped)
                break;
            Thread.Sleep(100);
        }

        return new List<Entry>(entries.ToArray());
    }
  • 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-05-18T12:39:59+00:00Added an answer on May 18, 2026 at 12:39 pm

    Looks like you’re never putting the Thread (t) in the List<Thread> (threads). Your foreach loop never executes.

    The main thread just waits 100ms and continues.

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

Sidebar

Related Questions

No related questions found

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.