A
alexia
Hi all,
I wrote a simple test app which using the Queue object. In some reson,
after several calling to Dequeue() I get this exception.
I have to threads. One thread adds to queue and the other removes from
queue.
I saw that the Queue is not ampty and if after the exception I call
Dequeue() again I don't get the exception.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
namespace queueTest
{
class Program
{
static Queue Q = new Queue();
static AutoResetEvent resEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Thread thrdA = new Thread(threadFunA);
Thread thrdB = new Thread(threadFunB);
thrdB.Start();
thrdA.Start();
Thread.Sleep(100000);
}
static void threadFunA()
{
int i = 1;
while (true)
{
Q.Enqueue(i);
Console.WriteLine("Writing: " + i.ToString());
resEvent.Set();
//If I put Thread.Sleep(1); the exception doesn't
happen.
i++;
}
}
static void threadFunB()
{
Queue sincQ = Queue.Synchronized(Q);
while (true)
{
try
{
resEvent.WaitOne();
Console.WriteLine("Reading: " + sincQ.Dequeue
().ToString());
}
catch (Exception e)
{
Console.WriteLine("Errror: " + e.Message);
}
}
}
}
}
Thanks for your help.
I wrote a simple test app which using the Queue object. In some reson,
after several calling to Dequeue() I get this exception.
I have to threads. One thread adds to queue and the other removes from
queue.
I saw that the Queue is not ampty and if after the exception I call
Dequeue() again I don't get the exception.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
namespace queueTest
{
class Program
{
static Queue Q = new Queue();
static AutoResetEvent resEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Thread thrdA = new Thread(threadFunA);
Thread thrdB = new Thread(threadFunB);
thrdB.Start();
thrdA.Start();
Thread.Sleep(100000);
}
static void threadFunA()
{
int i = 1;
while (true)
{
Q.Enqueue(i);
Console.WriteLine("Writing: " + i.ToString());
resEvent.Set();
//If I put Thread.Sleep(1); the exception doesn't
happen.
i++;
}
}
static void threadFunB()
{
Queue sincQ = Queue.Synchronized(Q);
while (true)
{
try
{
resEvent.WaitOne();
Console.WriteLine("Reading: " + sincQ.Dequeue
().ToString());
}
catch (Exception e)
{
Console.WriteLine("Errror: " + e.Message);
}
}
}
}
}
Thanks for your help.