INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problem with Logic / Textbook

Problem with Logic / Textbook

(OP)
Hi all,
This is a bit of a weird one.
I came across an example in my textbook and tried it out.
It is to do with:
Using the ConcurrentQueue<T> Collection
Here is the textbook example as provided ( DID NOT WORK )

CODE --> code

class Program 
{ 
   static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();   
   
   static void PlaceOrders() 
   { 
      for (int i = 1; i <= 100; i++) 
      { 
         Thread.Sleep(250); 
         String order = String.Format("Order {0}", i);          
         queue.Enqueue(order); 
         Console.WriteLine("Added {0}", order); 
      }    
   } 
   static void ProcessOrders() 
   { 
      	while (true) //continue indefinitely       
	if (queue.TryDequeue(out order)) 
      	{ 
                  Console.WriteLine("Processed {0}", order); 
      	}    
   }    
   static void Main(string[] args) 
   { 
      	var taskPlaceOrders = Task.Run(() => PlaceOrders()); 
      	Task.Run(() => ProcessOrders()); 
      	Task.Run(() => ProcessOrders());       
      	Task.Run(() => ProcessOrders());       
	taskPlaceOrders.Wait(); 
      	Console.WriteLine("Press ENTER to finish"); 
      	Console.ReadLine(); 
   } 
} 
Then I went and "fixed" it...

CODE --> worked

static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();
        private static string order;
       
        static void PlaceOrders()
        {
            
            for (int i = 1; i <= 100; i++)
            {
               
                Thread.Sleep(1000);
                String order = String.Format("Order {0}", i);
                queue.Enqueue(order);
                Console.WriteLine("Added {0}", order);
                
                

            }
        }

        static void ProcessOrders()
        {
            while (true) // continue indefinitely
            
                if (queue.TryDequeue(out order))
                {
                    Console.WriteLine("Processed {0}", order);   
                }
            
        } 
As you can see order is declared twice, and no error, and gave good output...
If I removed the String from in front of order ( the variable declared within the For loop ) it stopped working, it compiled, but the output was not working as desired.Below...

CODE --> String

class Program
    {
        #region Lesson 3
        #region Using the ConcurrentQueue<T> Collection

        static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();
        private static string order;
       
        static void PlaceOrders()
        {
            
            for (int i = 1; i <= 100; i++)
            {
               
                Thread.Sleep(1000);
                 order = String.Format("Order {0}", i);
                queue.Enqueue(order);
                Console.WriteLine("Added {0}", order);
                
                

            }
        }

        static void ProcessOrders()
        {
            while (true) // continue indefinitely
            
                if (queue.TryDequeue(out order))
                {
                    Console.WriteLine("Processed {0}", order);   
                }
            
        } 
There are some questions I would like to raise from the above:
1. I understand that the field order was required, but why is it necessary to declare it again as a String object in the for loop?
2. Why does the Add order ... not work if I remove the String keyword?
3. In the queue.TryDequeue( out order ) ), tryDequeue is accepting a parameter to test true or false right?

Thank you for the forthcoming answers smile
And thank you all for a great forum! thumbsup

Thank you,

Kind regards

Triacona

RE: Problem with Logic / Textbook

Hi Triacona,

I must admit I do not quite understand your problem. Have you "fixed" something else that is not mentioned here?
This here works just fine for me:

CODE --> C#

class Program
{
	static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

	private static string order;

	static void PlaceOrders()
	{
		for (int i = 1; i <= 20; i++)
		{
			Thread.Sleep(250);
			order = String.Format("Order {0}", i);
			queue.Enqueue(order);
			Console.WriteLine("Added {0}", order);
		}
	}
	static void ProcessOrders()
	{
		while (true) //continue indefinitely       
			if (queue.TryDequeue(out order))
			{
				Console.WriteLine("Processed {0}", order);
			}
	}
	static void Main(string[] args)
	{
		var taskPlaceOrders = Task.Run(() => PlaceOrders());
		Task.Run(() => ProcessOrders());
		Task.Run(() => ProcessOrders());
		Task.Run(() => ProcessOrders());
		taskPlaceOrders.Wait();
		Console.WriteLine("Press ENTER to finish");
		Console.ReadLine();
	}
} 

Best,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Problem with Logic / Textbook

(OP)
Thank you for you reply Thumbsup
But when you run your version the output is incorrect see below:
Added
Processed
Added
Processed Order 7
Added
Processed
Added
Processed
Added Order 10
Processed Order 10
Added Order 11
Processed Order 11

each line should have a number...
Thanks.

Thank you,

Kind regards

Triacona

RE: Problem with Logic / Textbook

Indeed! 3eyes
I will try to find out. Must have something to do with TryDequeue and what exactly it returns as well as with having a global variable. There can always be unexpected side-effects.
It's a weird one though.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Problem with Logic / Textbook

Got it!
It was indeed a side-effect of the global string variable. This here works just fine (really now), note the two bolded lines:

CODE --> code

class Program
{
	static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

	static void PlaceOrders()
	{
		string order;
		for (int i = 1; i <= 20; i++)
		{
			order = $"Order {i}";
			queue.Enqueue(order);
			Thread.Sleep(100);
			Console.WriteLine("Added {0}", order);
		}
	}
	static void ProcessOrders()
	{
		string test;
		while (true) //continue indefinitely       
			if (queue.TryDequeue(out test))
			{
				Console.WriteLine("Processed {0}", test);
			}
	}

	static void Main(string[] args)
	{
		var taskPlaceOrders = Task.Run(() => PlaceOrders());
		Task.Run(() => ProcessOrders());
		Task.Run(() => ProcessOrders());
		Task.Run(() => ProcessOrders());
		taskPlaceOrders.Wait();
		Console.WriteLine("Press ENTER to finish");
		Console.ReadLine();
	}
} 

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Problem with Logic / Textbook

(OP)
Thanks so very much!
This is an odd one.
So let me try get this straight in my head...

1. order is declared a string in the PlaceOrders() method.
2. order is assigned Order {i} and that is then added to the queue.
3. in the method ProcessOrders, another string is declared as order ( or your case test ).
4. queue is called using the queue.TryDequeue method which requires an out parameter.
5. the queue.TryDequeue method de-queues the queue object in the same order as added ( in first out first ).
6. the value is assigned to order ( or test ) using the out keyword?
7. .'. so in conclusion the method does 2 things, returns a bool and assigns the de-queued item to the variable given?

Thanks again for your help bigsmile

Thank you,

Kind regards

Triacona

RE: Problem with Logic / Textbook

Yes sort of. Only for point 3 it is different:
the string "test" is not declared as "order". Rather, "order" is already fixed into the queue, the "out ..." variable is simply a container that receives the returned value (of order).
So basically string test becomes a local copy of order.
By using a global variable you kept overwriting the value everytime you accessed the queue whileit was still running.
smile

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Problem with Logic / Textbook

(OP)
Thanks so much, much appreciated.
thumbsup

Thank you,

Kind regards

Triacona

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close