Bill,
This is a very classic producer - consumer issue. Multi-threading
programming 101.
I give you an example here, I get this from some body else's website.
Here a monitor is used. I believe there are other approaches too..
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Threadin g;
namespace ProduceConsumer
{
class Program
{
static ProducerConsume r _queue;
static void Main(string[] args)
{
_queue = new ProducerConsume r();
new Thread(new ThreadStart(Con sumerJob)).Star t();
Random rng = new Random(0);
for (int i = 0; i < 10; i++)
{
Console.WriteLi ne ("Producing {0}", i);
_queue.Produce( i);
Thread.Sleep(10 00);
}
}
static void ConsumerJob()
{
Random rng = new Random(1);
for (int i = 0; i < 10; i++)
{
int iConsume = _queue.Consume( );
Console.WriteLi ne("\t\t\tConsu ming {0}", iConsume);
Thread.Sleep(rn g.Next(1000));
}
}
}
public class ProducerConsume r
{
readonly object listLock = new object();
Queue<int> queue = new Queue<int>();
public void Produce(int i)
{
lock (listLock)
{
queue.Enqueue(i );
if (queue.Count == 1)
{
Monitor.Pulse(l istLock);
}
}
}
public int Consume()
{
lock (listLock)
{
while (queue.Count == 0)
{
Monitor.Wait(li stLock);
}
return queue.Dequeue() ;
}
}
}
}
bill wrote:
How would I signal the main app when a thread has completed a task.
The thread must remain actice. Every time it reads a given number of bytes
it must let the main app know, then wait until more data shows up. The
number of bytes it reads varies.
Thanks,
Bill