i want to implement multiple thread Producer Consumer, my purpose is insert strings into my Queue
, each string represent DOC file
that need to be check (simple search inside the document) and in this check is OK the doc file can be add into my ListView
.
public class ProducerConsumer
{
public delegate void OnFileAddDelegate(string file);
public event OnFileAddDelegate OnFileAddEventHandler;
private BlockingCollection<string> queue = new BlockingCollection<string>();
public void Start(int workerCount)
{
int count = 0;
while (count < workerCount)
{
Thread thread = new Thread(StartConsuming);
thread.IsBackground = true;
thread.Start();
count++;
}
}
public void Produce(string item)
{
queue.Add(item);
}
private void StartConsuming()
{
while (queue.Count > 0)
{
string item = queue.Take();
// Check my file
FileChecker fileChecker = new FileChecker();
string result = fileChecker.Check(item);
// If the file is OK fire up an event to my main form to add this file
if (result != null && OnFileAddEventHandler != null)
OnFileAddEventHandler(result);
}
}
}
Now i want add to my class the option to add files in multiple thread. Any suggestions how to do that ?