Pids ProcessHandleCache::Cleanup()
{
	Pids removePids;
	for (auto i = m_cache.begin(); i != m_cache.end(); ++i)
	{
		DWORD exitcode = 0;
		BOOL result = GetExitCodeProcess(i->second.get(), &exitcode);
		if (result == FALSE || exitcode != STILL_ACTIVE)
		{
			DWORD pid = i->first;
			removePids.push_back(pid);
		}
	}

	for (auto i = removePids.begin(); i != removePids.end(); ++i)
		m_cache.erase(*i);
	return removePids;
}
Lines DBWinReader::CheckHandleCache()
{
	if ((m_timer.Get() - m_handleCacheTime) < HandleCacheTimeout)
		return Lines();

	Lines lines;
	Pids removedPids = m_handleCache.Cleanup();
	for (auto i = removedPids.begin(); i != removedPids.end(); i++)
	{
		DWORD pid = *i;
		if (m_lineBuffers.find(pid) != m_lineBuffers.end())
		{
			if (!m_lineBuffers[pid].empty())
				lines.push_back(Line(m_timer.Get(), GetSystemTimeAsFileTime(), pid, "<flush>", m_lineBuffers[pid]));
			m_lineBuffers.erase(pid);
		}
	}
	m_handleCacheTime = m_timer.Get();
	return lines;
}