Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串传递给接收操作的函数_C#_.net_Action - Fatal编程技术网

C# 将字符串传递给接收操作的函数

C# 将字符串传递给接收操作的函数,c#,.net,action,C#,.net,Action,我从以下方面举例说明了BlockingCollection: 这是用户选择要添加的文件后我的主要表单,我想将这些文件添加到我的队列中: 字符串[]文件 PCQueue pq = new PCQueue(1); pq.OnFileAddEventHandler += pq_OnFileAddEventHandler; foreach (string item in openFileDialog1.FileNames)

我从以下方面举例说明了BlockingCollection:

这是用户选择要添加的文件后我的主要表单,我想将这些文件添加到我的
队列中

字符串[]文件

            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
    }
但我有一个错误:无法从“string”转换为“System.Action”
尽管我看到了这篇文章,但我无法解决它(我是一名新的开发人员)

您正在定义您的集合以获取任务对象。由于新的使用者类处理文件名,只需将其更改为接受字符串(
BlockingCollection
),并相应地重新编写其余代码。(
foreach(taskQ.getconsumineGenumerable()中的字符串文件名)
)等。

Action
是一个委托,这意味着您必须传递一个方法或lambda表达式,该表达式返回
void
,并将
string
作为参数。委托基本上是C++所知道的类型安全的函数指针,所以不能通过<代码>字符串< /Cord>对象。互联网上有很多关于它的信息,例如,你的foreach循环需要一个外壳,或者它只是要创建一堆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);
        }
    }
            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
    }