예제 #1
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
                                          EventWaitHandle threadActivatedEvent, ThreadTermination threadTermination)
        {
            return(state =>
            {
                try
                {
                    var processEvents = new WaitHandle[]
                    {
                        block.CommunicationBlock.ProfilerRequestsInformation,
                        block.MemoryBlock.ProfilerHasResults,
                        threadTermination.CancelThreadEvent
                    };
                    threadActivatedEvent.Set();

                    try
                    {
                        if (ProcessActiveBlock(block, processEvents))
                        {
                            return;
                        }
                        _memoryManager.RemoveDeactivatedBlock(block);
                    }
                    finally
                    {
                        threadTermination.ThreadFinishedEvent.Set();
                    }
                }
                catch (ObjectDisposedException)
                {
                    /* an attempt to close thread has probably happened and the events disposed */
                }
            });
        }
예제 #2
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
                                          EventWaitHandle threadActivatedEvent, ThreadTermination threadTermination)
        {
            return(state =>
            {
                try
                {
                    var processEvents = new WaitHandle[]
                    {
                        block.CommunicationBlock.ProfilerRequestsInformation,
                        block.MemoryBlock.ProfilerHasResults,
                        threadTermination.CancelThreadEvent
                    };
                    threadActivatedEvent.Set();

                    try
                    {
                        while (block.Active)
                        {
                            switch (WaitHandle.WaitAny(processEvents))
                            {
                            case 0:
                                _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                                break;

                            case 1:
                                var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                                // don't let the queue get too big as using too much memory causes
                                // problems i.e. the target process closes down but the host takes
                                // ages to shutdown; this is a compromise.
                                _messageQueue.Enqueue(data);
                                if (_messageQueue.Count > 400)
                                {
                                    do
                                    {
                                        ThreadHelper.YieldOrSleep(100);
                                    } while (_messageQueue.Count > 200);
                                }
                                break;

                            default:     // 2
                                return;
                            }
                        }
                        _memoryManager.RemoveDeactivatedBlock(block);
                    }
                    finally
                    {
                        threadTermination.ThreadFinishedEvent.Set();
                    }
                }
                catch (ObjectDisposedException)
                {
                    /* an attempt to close thread has probably happened and the events disposed */
                }
            });
        }