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 6389499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:24:39+00:00 2026-05-25T03:24:39+00:00

I have a class as: class SomeClass{ class Connection{//some fields} static List<Connection> connections {

  • 0

I have a class as:

class SomeClass{

    class Connection{//some fields}

    static List<Connection> connections { get; set; }


     public SomeClass( \\params etc)
     {

       connections = new List<Connections>(); // initialize connections list

      //initialize some other private vars
      // ...

       mainClassThreadMethod();
     }

    private void mainClassThreadMethod()
    {
        while (true)
        {

            Thread t;
            Connection p = new Connection ( { \\instantiate the class})
            // this code will not execute until p is initialized...  In other words this loop will not execute several times quickly. 

            t = new Thread(new ParameterizedThreadStart(startThread));
            t.Start(p);
        }
    }



    private void startThread(object o)
    {
        //add a new connection to the list
        connections.Add((Connection)o));
    }

    public List<Connection> getConnections()
    {
        return connections;
    }

}

why is it that after adding new connections to the list if I then call the getConnections method it returns a null list? I figure it is because I am adding the items from a different thread. How can I keep track of this?

  • 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-25T03:24:39+00:00Added an answer on May 25, 2026 at 3:24 am

    You have several problems in the code above, but sticking to the question asked, to synchronize the list (allow adding from different threads) you can (1) implement your own locking, or (2) use http://msdn.microsoft.com/en-us/library/3azh197k.aspx.

    I’d go for #2, but in your case:

    • No, it’s probably not caused by adding anything from a different thread
    • Why would you even want to add from inside startThread? You have the connection object before you instantiate a thread, so you can easily call connections.Add(connection) from the same thread thereby eliminating the need for any locking.
    • Why is there a while(true) loop around the thread spin-up process?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have some Java code: public class SomeClass { static { private
I have the following class: public class SomeClass { public double SomeValue {get;set;} }
Suppose I have a class that processes some data: class SomeClass { public: void
I have this class package somePackage { public class SomeClass { public static const
Suppose I have this code (pseudocode) class SomeClass { class Person { public static
I have a class that looks like this public class SomeClass { public SomeChildClass[]
So I have a class with a method as follows: public class SomeClass {
I have seen some C++ classes with a destructor defined as follows: class someClass
I have a class like this class someclass{ public function somemethod(){} } Now I
my problem is here: I have some class public class Component { ... private

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.