Exemplo n.º 1
0
        private void ProcessMessages(IEnumerable <WaitHandle> handles)
        {
            var threadHandles = new List <Tuple <ManualResetEvent, ManualResetEvent> >();

            do
            {
                switch (WaitHandle.WaitAny(handles.ToArray()))
                {
                case 0:
                    _continueWait = false;
                    break;

                case 1:
                    _communicationManager.HandleCommunicationBlock(_mcb, (mcb, mmb) => threadHandles.Add(StartProcessingThread(mcb, mmb)));
                    break;
                }
            } while (_continueWait);

            foreach (var block in _memoryManager.GetBlocks.Select(b => b.Item2))
            {
                var data = new byte[block.BufferSize];
                block.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
                block.StreamAccessorResults.Read(data, 0, block.BufferSize);
                _messageQueue.Enqueue(data);
            }

            if (threadHandles.Any())
            {
                threadHandles.Select(h => h.Item1).ToList().ForEach(h => h.Set());
                WaitHandle.WaitAll(threadHandles.Select(h => h.Item2).Cast <WaitHandle>().ToArray());
            }

            _messageQueue.Enqueue(new byte[0]);
        }
Exemplo n.º 2
0
        private void ProcessMessages(IEnumerable <WaitHandle> handles)
        {
            var mainEvents = new List <WaitHandle>(handles);

            do
            {
                switch (WaitHandle.WaitAny(mainEvents.ToArray()))
                {
                case 0:
                    _continueWait = false;
                    break;

                case 1:
                    _communicationManager.HandleCommunicationBlock(_mcb, StartProcessingThread);
                    break;
                }
            } while (_continueWait);

            foreach (var block in _memoryManager.GetBlocks.Select(b => b.Item2))
            {
                var data = new byte[block.BufferSize];
                block.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
                block.StreamAccessorResults.Read(data, 0, block.BufferSize);
                _messageQueue.Enqueue(data);
            }

            _messageQueue.Enqueue(new byte[0]);
        }
Exemplo n.º 3
0
        private void ProcessMessages(WaitHandle[] handles)
        {
            var threadHandles = new List <ThreadTermination>();

            do
            {
                switch (WaitHandle.WaitAny(handles))
                {
                case 0:
                    _continueWait = false;
                    break;

                case 1:
                    _communicationManager.HandleCommunicationBlock(_mcb,
                                                                   block => Task.Factory.StartNew(() =>
                    {
                        lock (SyncRoot)
                        {
                            threadHandles.Add(StartProcessingThread(block));
                        }
                    }));
                    break;

                default:
                    break;
                }
            } while (_continueWait);

            _memoryManager.WaitForBlocksToClose(BufferWaitCount);

            _memoryManager.FetchRemainingBufferData(data => _messageQueue.Enqueue(data));

            lock (SyncRoot)
            {
                if (threadHandles.Any())
                {
                    var tasks = threadHandles
                                .Select((e, index) => new { ThreadTermination = e, Block = index / NumHandlesPerBlock })
                                .GroupBy(g => g.Block)
                                .Select(g => g.Select(a => a.ThreadTermination).ToList())
                                .Select(g => Task.Factory.StartNew(() =>
                    {
                        ConsumeException(() =>
                        {
                            g.Select(h => h.CancelThreadEvent).ToList().ForEach(h => h.Set());
                            WaitHandle.WaitAll(g.Select(h => h.ThreadFinishedEvent).ToArray <WaitHandle>(),
                                               new TimeSpan(0, 0, 20));
                        });
                    })).ToArray();

                    Task.WaitAll(tasks);

                    threadHandles.Clear();
                }
            }

            _messageQueue.Enqueue(new byte[0]);
        }
Exemplo n.º 4
0
        private void ProcessMessages(WaitHandle[] handles)
        {
            var threadHandles = new List <Tuple <EventWaitHandle, EventWaitHandle> >();

            do
            {
                switch (WaitHandle.WaitAny(handles))
                {
                case 0:
                    _continueWait = false;
                    break;

                case 1:
                    _communicationManager.HandleCommunicationBlock(_mcb, block => threadHandles.Add(StartProcessingThread(block)));
                    break;
                }
            } while (_continueWait);

            foreach (var block in _memoryManager.GetBlocks.Where(b => b.Active).Select(b => b.MemoryBlock))
            {
                var data = new byte[block.BufferSize];
                block.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
                block.StreamAccessorResults.Read(data, 0, block.BufferSize);
                _messageQueue.Enqueue(data);
            }

            if (threadHandles.Any())
            {
                var tasks = threadHandles
                            .Select((e, index) => new { Pair = e, Block = index / NumHandlesPerBlock })
                            .GroupBy(g => g.Block)
                            .Select(g => g.Select(a => a.Pair).ToList())
                            .Select(g => Task.Factory.StartNew(() =>
                {
                    g.Select(h => h.Item1).ToList().ForEach(h => h.Set());
                    WaitHandle.WaitAll(g.Select(h => h.Item2).ToArray <WaitHandle>(), new TimeSpan(0, 0, 20));
                })).ToArray();
                Task.WaitAll(tasks);

                foreach (var threadHandle in threadHandles)
                {
                    threadHandle.Item1.Dispose();
                    threadHandle.Item2.Dispose();
                }
                threadHandles.Clear();
            }

            _messageQueue.Enqueue(new byte[0]);
        }
Exemplo n.º 5
0
        private void ProcessMessages(WaitHandle[] handles)
        {
            var threadHandles = new List <Tuple <EventWaitHandle, EventWaitHandle> >();

            do
            {
                switch (WaitHandle.WaitAny(handles))
                {
                case 0:
                    _continueWait = false;
                    break;

                case 1:
                    _communicationManager.HandleCommunicationBlock(_mcb,
                                                                   block => Task.Factory.StartNew(() =>
                    {
                        lock (threadHandles)
                        {
                            threadHandles.Add(StartProcessingThread(block));
                        }
                    }));
                    break;
                }
            } while (_continueWait);

            // we need to let the profilers dump the thread buffers over before they close - max 15s (ish)
            var i = 0;

            while (i < BufferWaitCount && _memoryManager.GetBlocks.Any(b => b.Active))
            {
                DebugLogger.InfoFormat("Waiting for {0} processes to close", _memoryManager.GetBlocks.Count(b => b.Active));
                Thread.Sleep(500);
                i++;
            }

            // grab anything left in the main buffers
            foreach (var block in _memoryManager.GetBlocks.Where(b => b.Active).Select(b => b.MemoryBlock))
            {
                var data = new byte[block.BufferSize];
                block.StreamAccessorResults.Seek(0, SeekOrigin.Begin);
                block.StreamAccessorResults.Read(data, 0, block.BufferSize);
                _messageQueue.Enqueue(data);
            }

            lock (threadHandles)
            {
                if (threadHandles.Any())
                {
                    var tasks = threadHandles
                                .Select((e, index) => new { Pair = e, Block = index / NumHandlesPerBlock })
                                .GroupBy(g => g.Block)
                                .Select(g => g.Select(a => a.Pair).ToList())
                                .Select(g => Task.Factory.StartNew(() =>
                    {
                        g.Select(h => h.Item1).ToList().ForEach(h => h.Set());
                        WaitHandle.WaitAll(g.Select(h => h.Item2).ToArray <WaitHandle>(), new TimeSpan(0, 0, 20));
                    })).ToArray();
                    Task.WaitAll(tasks);

                    foreach (var threadHandle in threadHandles)
                    {
                        threadHandle.Item1.Dispose();
                        threadHandle.Item2.Dispose();
                    }
                    threadHandles.Clear();
                }
            }

            _messageQueue.Enqueue(new byte[0]);
        }