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 )
Then I went and "fixed" it...
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...
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
And thank you all for a great forum!
Thank you,
Kind regards
Triacona
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:
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();
}
}
Code:
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);
}
}
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:
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);
}
}
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
And thank you all for a great forum!
Thank you,
Kind regards
Triacona