rangerasyncresult.h

来自「支票扫描仪协议Ranger的应用.使用vc7.1」· C头文件 代码 · 共 88 行

H
88
字号

#pragma once 

#include <afxmt.h>
 
template <class T>
class CRangerAsyncResult
{
public:

	// check result(call in host).
	// initialize 
	void Initialize(const T& init_val)
	{
		m_eResult.ResetEvent();
		m_Result = init_val;
	}

	// check 
	BOOL CheckResult(T& result,DWORD dwTimeout = 8000)
	{
		BOOL bQuitLoop = FALSE;
		do
		{
			DWORD rs_wait =::MsgWaitForMultipleObjects(1, &m_eResult.m_hObject, FALSE, dwTimeout, QS_ALLINPUT);
			if (rs_wait == WAIT_OBJECT_0)
			{
				bQuitLoop = TRUE;;
			}
			else if( rs_wait == (WAIT_OBJECT_0+1) )
			{
				MSG msg;
				while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
					if (msg.message == WM_QUIT){
						bQuitLoop = TRUE;
 						break;
					}
					::TranslateMessage(&msg);
					::DispatchMessage(&msg);

					DWORD ret = ::WaitForSingleObject(m_eResult.m_hObject, 0);
					if ( ret  == WAIT_OBJECT_0) 
					{
						bQuitLoop = TRUE;
						break;
					}
				}
			}

			else
			{
				return FALSE;
			}
		}while(!bQuitLoop);
 

		CSingleLock lck(&m_csResult);
		if (!lck.Lock()) 
			return FALSE;		// Time Out

		result = m_Result;

		return TRUE;
	}

	// set result (call in client).
	BOOL SetResult(const T& result)
	{
		CSingleLock lck(&m_csResult);
		if (!lck.Lock()) 
			return FALSE;			// Time Out

		m_Result = result;
	
		lck.Unlock();

		// notify result has been changed
		m_eResult.SetEvent();
		
		return TRUE;

	}
protected:
	T m_Result;

	CEvent				m_eResult;
	CCriticalSection	m_csResult;
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?