private WaitCallback SaveVisitData(EventWaitHandle queueMgmt) { return(state => { while (true) { byte[] data; while (!_messageQueue.TryDequeue(out data)) { ThreadHelper.YieldOrSleep(100); } _perfCounters.CurrentMemoryQueueSize = _messageQueue.Count; _perfCounters.IncrementBlocksReceived(); if (data.Length == 0) { _communicationManager.Complete(); queueMgmt.Set(); return; } _persistance.SaveVisitData(data); } }); }
public void RunProcess(Action <Action <StringDictionary> > process, bool isService) { var key = Guid.NewGuid().GetHashCode().ToString("X"); var processMgmt = new AutoResetEvent(false); var queueMgmt = new AutoResetEvent(false); var environmentKeyRead = new AutoResetEvent(false); var handles = new List <WaitHandle> { processMgmt }; string @namespace = isService ? "Global" : "Local"; _memoryManager.Initialise(@namespace, key); _messageQueue = new ConcurrentQueue <byte[]>(); using (_mcb = new MemoryManager.ManagedCommunicationBlock(@namespace, key, maxMsgSize, -1)) { handles.Add(_mcb.ProfilerRequestsInformation); ThreadPool.QueueUserWorkItem((state) => { try { process(dictionary => { if (dictionary == null) { return; } dictionary[@"OpenCover_Profiler_Key"] = key; dictionary[@"OpenCover_Profiler_Namespace"] = @namespace; dictionary[@"OpenCover_Profiler_Threshold"] = _commandLine.Threshold.ToString(CultureInfo.InvariantCulture); if (_commandLine.TraceByTest) { dictionary[@"OpenCover_Profiler_TraceByTest"] = "1"; } dictionary["Cor_Profiler"] = "{1542C21D-80C3-45E6-A56C-A9C1E4BEB7B8}"; dictionary["Cor_Enable_Profiling"] = "1"; dictionary["CoreClr_Profiler"] = "{1542C21D-80C3-45E6-A56C-A9C1E4BEB7B8}"; dictionary["CoreClr_Enable_Profiling"] = "1"; if (_commandLine.Registration == Registration.Path32) { dictionary["Cor_Profiler_Path"] = ProfilerRegistration.GetProfilerPath(false); } if (_commandLine.Registration == Registration.Path64) { dictionary["Cor_Profiler_Path"] = ProfilerRegistration.GetProfilerPath(true); } environmentKeyRead.Set(); }); } finally { processMgmt.Set(); } }); ThreadPool.QueueUserWorkItem((state) => { while (true) { //// use this block to introduce a delay in to the queue processing //if (_messageQueue.Count < 100) // Thread.Sleep(10); byte[] data; while (!_messageQueue.TryDequeue(out data)) { Thread.Yield(); } _perfCounters.CurrentMemoryQueueSize = _messageQueue.Count; _perfCounters.IncrementBlocksReceived(); if (data.Length == 0) { _communicationManager.Complete(); queueMgmt.Set(); return; } _persistance.SaveVisitData(data); } }); // wait for the environment key to be read if (WaitHandle.WaitAny(new WaitHandle[] { environmentKeyRead }, new TimeSpan(0, 0, 0, 10)) != -1) { ProcessMessages(handles); queueMgmt.WaitOne(); } } }