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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:10+00:00 2026-05-27T16:10:10+00:00

I have an infinite loop in a separate thread operating on a List of

  • 0

I have an infinite loop in a separate thread operating on a List of strings. I want to be able to add strings to this list while the thread is runnning. I have a feeling the code I am writing is ‘wrong’. In the infinite loop I am iterating through each string in the list and performing operations on it, so it seems like I can’t just add a string to this list from my main thread, as I will be interfering with a variable that is concurrently being accessed by another thread. Here’s what my code looks like –

class StringTest
{
public List<string> ListOfStrings = new List<string>();
public Task MainLoopTask;
bool IsRunning = false;

public void AddToList(string myString)
{
    ListOfStrings.Add(myString); // Adding a string to the list

    if (!IsRunning)
    {
        IsRunning = true;
        MainLoopTask = Task.Factory.StartNew(MainLoop);
    }
}

public void MainLoop()
{
    while (true)
    {
        foreach(string s in ListOfStrings) // Operating on the list in a separate thread
        {
            ...
            ...
            ...
        }
}

}

Is this bad code or is it ok? If it’s is bad, what can I do to fix it?

  • 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-27T16:10:10+00:00Added an answer on May 27, 2026 at 4:10 pm

    That is not safe, and will eventually fail spectacularly in production.

    Instead, you should use a thread-safe collection; probably a concurrent queue.

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

Sidebar

Related Questions

I have a PHP script with infinite loop. I need this script running forever.
If I have a thread in an infinite loop, is there a way to
I have an infinite loop that I want to sleep for a second but
I hava a thread in which I have an infinite loop, doing some network
iPhone Development: I want to have an infinite loop constantly checking an NSMutableArray and
I have a simple infinite for loop looking like this: set_time_limit (0); for (;;)
I have a simple thread that runs an infinite loop. Eventually, the loop will
I have two iterators, a list and an itertools.count object (i.e. an infinite value
When redirect to my home page I have an infinite loop. For example is
I have two versions of a minimal web server. Both execute an infinite loop

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.