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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:07:11+00:00 2026-05-14T17:07:11+00:00

I have a program (C#) with a list of tests to do. Also, I

  • 0

I have a program (C#) with a list of tests to do.
Also, I have two thread. one to add task into the list, and one to read and remove from it the performed tasks.
I’m using the ‘lock’ function each time one of the threads want to access to the list.
Another thing I want to do is, if the list is empty, the thread who need to read from the list will sleep. and wake up when the first thread add a task to the list.
Here is the code I wrote:

...
List<String> myList = new List();
Thread writeThread, readThread;
writeThread = new Thread(write);
writeThread.Start();
readThraed = new Thread(read);
readThread.Start();
...
private void write()
{
   while(...)
   {
     ...
     lock(myList)
     {
        myList.Add(...);
     }
     ...
     if (!readThread.IsAlive)
     {
        readThraed = new Thread(read);
        readThread.Start();
     }
     ...
   }
   ...
}

private void read()
{
bool noMoreTasks = false;
   while (!noMoreTasks)
   {
      lock (MyList)//syncronize with the ADD func.
      {
                if (dataFromClientList.Count > 0)
                {
                    String task = myList.First();
                    myList.Remove(task);
                }
                else
                {
                    noMoreTasks = true;
                }      
      }
      ...
   }
   readThread.Abort();
}

Apparently I did it wrong, and it’s not performed as expected (The readTread does’t read from the list).
Does anyone know what is my problem, and how to make it right?
Many thanks,

  • 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-14T17:07:12+00:00Added an answer on May 14, 2026 at 5:07 pm

    What you need is a blocking queue. It is like a normal queue except the Dequeue method blocks if nothing is queued. Here is one implementation. Once you get the blocking queue implemented then everything else is easy. Just be careful which implementation of the blocking queue you use. I have seen a lot of examples out there that have subtle threading problems. It might be best to stick with the link I provided.

    public class Example
    {
      private BlockingQueue<Task> m_Queue = new BlockingQueue<Task>();
    
      public void StartExample()
      {
        Thread producer = new Thread(() => Producer());
        Thread consumer = new Thread(() => Consumer());
        producer.Start();
        consumer.Start();
        producer.Join();
        consumer.Join();
      }
    
      private void Producer()
      {
        for (int i = 0; i < 10; i++)
        {
          m_Queue.Enqueue(new Task());
        }
      }
    
      private void Consumer()
      {
        while (true)
        {
          Task task = m_Queue.Dequeue();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small Java program that reads list of IPs from text file
I have a program that needs to read data from a number of sources.
i have recently developed a list program in android in which there is an
I have a program which places structures in a linked list based on the
I have a C# program that displays a list of patients with medications where
I have a Matlab program that generates a list x = 6.1692 8.1863 5.8092
I have a chat program that displays I list of online users. The message
I have a C# program with an interrupt that processes part of a list
I have a Java program that is supplied a directory name, gets a list
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>

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.