2

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)

Community
  • 1
  • 1
may danit
  • 25
  • 6
  • `Action` is a delegate, which means you have to pass a method or lambda expression, that returns `void` and takes `string` as an argument. Delegates are basically type-safe function pointers known from C++, so you can't pass `string` object. There's a lot about it on the Internet, e.g. http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/what-is-action-in-c-sharp/. – Bartłomiej Zieliński Aug 29 '14 at 17:50
  • Also, your foreach loop needs an enclosure or it's just going to create a bunch of checker objects and only use the last one. – C Bauer Aug 29 '14 at 17:56
  • See my update (class and main form), is it OK new ? BTW this class support multiple threads Consumer ? – may danit Aug 29 '14 at 18:02
  • I'm a little confused. Did you update your code to match the answer's suggestions? Does this updated code still not work? Not sure if you still need an answer or which code I should be looking at for errors. – DCShannon Aug 29 '14 at 18:23
  • I am sorry that i didn't mention that this code works perfect now – may danit Aug 29 '14 at 18:44

1 Answers1

0

you are defining your collection to take Task objects. since your new consumer class processes filenames, just change it to accept strings (BlockingCollection<string>), and rework the rest of your code accordingly. (foreach (string filename in _taskQ.GetConsumingEnumerable())) etc..

ths
  • 2,858
  • 1
  • 16
  • 21