Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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#_Task Parallel Library_Task_Producer Consumer - Fatal编程技术网

C# 任务不存在';我不能并行工作

C# 任务不存在';我不能并行工作,c#,task-parallel-library,task,producer-consumer,C#,Task Parallel Library,Task,Producer Consumer,我有两个任务,作为生产者和消费者,但它们不是并行运行的。第二个等待第一个完成。你能解释一下我为什么以及如何纠正这个问题吗?我想让它们同时运行。(我尝试了很多Varian,但都不太好用) 如果我没有错的话,您需要更改isAvailable.Set();到isAvailable.ReSet();为了将锁设置为阻塞,您应该使用Reset和not set。不要试图分析错误,但是如果您使用TPL,您很少(如果有)需要AutoResetEvent。这段代码需要重新分解。考虑使用 TaskCuleTythOr

我有两个任务,作为生产者和消费者,但它们不是并行运行的。第二个等待第一个完成。你能解释一下我为什么以及如何纠正这个问题吗?我想让它们同时运行。(我尝试了很多Varian,但都不太好用)


如果我没有错的话,您需要更改isAvailable.Set();到isAvailable.ReSet();为了将锁设置为阻塞,您应该使用Reset和not set。不要试图分析错误,但是如果您使用TPL,您很少(如果有)需要
AutoResetEvent
。这段代码需要重新分解。考虑使用<代码> TaskCuleTythOrths<代码>和<代码> BuffjCopys<代码>。更进一步的一步是使用TPL数据流。@KobyYehezkel我试图使用
isAvailable.Reset()
而不是
isAvailable.Set()
,但没有帮助me@Noseratio我使用
BlockingCollection
重写了代码,但仍然不起作用。您如何准确地观察它们是否并行运行?
class DirectoryReader
    {
        private readonly string _dir ;
        private Processor[] _processors;
        private string[] _files;
        private readonly Regex _rx = new Regex(@"([^.\\]+)\.cs");

        private Queue<Processor> queue = new Queue<Processor>();
        private bool isOff;
        private AutoResetEvent isAvailable = new AutoResetEvent(false);
        private StreamWriter log = new StreamWriter("Log.txt");



        public DirectoryReader(string dir)
        {
            _dir = dir; 
        }

        public Container[] ProcessAllFiles()
        {
            _files = Directory.GetFiles(_dir, "*.cs", SearchOption.AllDirectories);
            _processors = new Processor[_files.Length];                   

            var thread = Task.Run(() => Compute());
            var thread2 = Task.Run(() => Read());           
            thread.Wait();          
        }

        public void Read()
        {
                for (var i = 0; i < _files.Length; i++)
                {
                    try
                    {
                        var matches = _rx.Matches(_files[i]);
                        foreach (var match in matches)
                        {
                            //Console.WriteLine(match);
                            lock (log)
                            {
                                log.WriteLine(match);
                            }
                        }
                        _processors[i] = new Processor(matches[matches.Count - 1].ToString(), File.ReadAllText(_files[i]));
                        lock (queue)
                        {
                            queue.Enqueue(_processors[i]);
                            isAvailable.Set();
                        }

                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine(ex.Message + _files[i]);
                    }

                }
                isOff = true;
            }

        public void Compute()
        {
            Processor proc = null;
            int ccount = 0;
            while (true)
            {
                lock (queue)
                {
                    if ((ccount = queue.Count) > 0)
                    {
                        proc = queue.Dequeue();
                    }
                }
                if (ccount == 0)
                {
                    if (isOff)
                    {
                        return;
                    }
                    else
                    {
                        isAvailable.WaitOne();
                    }
                }
                if (proc != null)
                {
                    //Some calculations on proc
                    lock (log)
                    {
                        log.WriteLine("+++++" + proc.FileName);
                    }
                }

            }
        }
    }
class DirectoryReader
    {
        private readonly string _dir ;
        private Processor[] _processors;
        private string[] _files;
        private readonly Regex _rx = new Regex(@"([^.\\]+)\.cs");
        private List<Container> answer = new List<Container>();       
        BlockingCollection<FileContainer> dataItems = new BlockingCollection<FileContainer>();

        public DirectoryReader(string dir)
        {
            _dir = dir; 
        }

        public void ProcessAllFiles()
        {
            _files = Directory.GetFiles(_dir, "*.cs", SearchOption.AllDirectories);
            _processors = new Processor[_files.Length];

            var task = Task.Factory.StartNew(Compute);
            var task2 = Task.Factory.StartNew(Read);
            Task.WaitAll(task, task2);
        }

        public void Read()
        {
            for (var i = 0; i < _files.Length; i++)
            {
                try
                {
                    var matches = _rx.Matches(_files[i]);                                          
                    dataItems.Add(new FileContainer{
                        Name = matches[matches.Count - 1].ToString(),
                        Content = File.ReadAllText(_files[i])});                       
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message + _files[i]);
                }

            }
            dataItems.CompleteAdding();
        }

        public void Compute()
        {
            FileContainer proc = null;
            while (!dataItems.IsCompleted)
            {
                try
                {
                    proc = dataItems.Take();
                }
                catch (InvalidOperationException) { }
                if (proc != null)
                {
                    //Some calculations
                }
            }
        }
    }