I took this example of BlockingCollection from here:
public class PCQueue : IDisposable
{
public delegate void OnFileAddDelegate(string file);
public event OnFileAddDelegate OnFileAddEventHandler;
BlockingCollection<string> _taskQ = new BlockingCollection<string>();
public PCQueue(int workerCount)
{
// Create and start a separate Task for each consumer:
for (int i = 0; i < workerCount; i++)
Task.Factory.StartNew(Consume);
}
public void Dispose()
{
_taskQ.CompleteAdding();
}
public void EnqueueTask(string action)
{
_taskQ.Add(action);
}
void Consume()
{
// This sequence that we’re enumerating will block when no elements
// are available and will end when CompleteAdding is called.
FileChecker fileChecker = new FileChecker();
foreach (string item in _taskQ.GetConsumingEnumerable())
{
string file = item;
string result = fileChecker.Check(file);
if (result != null && OnFileAddEventHandler != null)
OnFileAddEventHandler(result);
}
}
}
What i want to do is very simple, I have Winforms
application with ListView
so the user choose several files (PDF files) and i want this class to store this files that the user choose and Check this files via another class that i have (simple search inside each file)
and if the file is OK i fire up event (new event added) to my main form in order to add this file.
So this is my new Consume
function with my file checker:
void Consume()
{
// This sequence that we’re enumerating will block when no elements
// are available and will end when CompleteAdding is called.
FileChecker fileChecker = new FileChecker();
foreach (string item in _taskQ.GetConsumingEnumerable())
{
string file = item;
string result = fileChecker.Check(file);
if (result != null && OnFileAddEventHandler != null)
OnFileAddEventHandler(result);
}
}
And this is my main form after the user choose files to add and i want to add this files into my Queue
:
string[] files;
PCQueue pq = new PCQueue(1);
pq.OnFileAddEventHandler += pq_OnFileAddEventHandler;
foreach (string item in openFileDialog1.FileNames)
{
string filename = item;
pq.EnqueueTask(filename);
}
private void pq_OnFileAddEventHandler(string file)
{
// Add my file
}
But i have this error: cannot convert from 'string' to 'System.Action' And although i saw this post i cannot solve it (i am a new developer)