⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.cpp

📁 fax engine 传真引擎 relay fax 的开源项目 商业软件使用 高质量 高可靠
💻 CPP
字号:
/*****************************************************************************
* RelayFax Open Source Project
* Copyright 1996-2004 Alt-N Technologies, Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the RelayFax Open 
* Source License.  A copy of this license is available in file LICENSE 
* in the top-level directory of the distribution.
*
* RelayFax is a registered trademark of Alt-N Technologies, Ltd.
*
* Individual files and/or contributed packages may be copyright by
* other parties and subject to additional restrictions.
*****************************************************************************/

#include "stdafx.h"
#include "Thread.h"
#include "excepthandler.h"

//////////////////////////////// Externals /////////////////////////////////////

////////////////////// Static Member Functions //////////////////////////
// Static thread function calls member function, since _beginthreadex
// can't call a member function directly, it passes the object pointer
// to this static function
unsigned int _stdcall CThread::ThreadFunc( void* pArg )
{
	CThread* pThread = (CThread*) pArg;

	return pThread->Run();
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CThread::CThread( void )
{
	// Initialize thread handle
	m_hThread = INVALID_HANDLE_VALUE;
	m_nWaitTimeout = INFINITE;
}

CThread::~CThread()
{
	// Close all handles used by thread
	if ( m_hThread != INVALID_HANDLE_VALUE )
		CloseHandle( m_hThread );
}

/////////////////////////// Public member functions //////////////////

// Thread startup
// 
DWORD CThread::StartThread( void )
{
	m_dwLastTimeout = GetTickCount();

	// Create the thread
	m_hThread = (HANDLE)_beginthreadex( NULL, 0, CThread::ThreadFunc, 
		                                this, 0, &m_nThreadID ); 

	if ( m_hThread == INVALID_HANDLE_VALUE )
	{
		// Error creating thread
		return -1;
	}
	
	// Success
	return 0;
}


unsigned int CThread::Run()
{
	DWORD dwResult;
	bool bRun;
	
	__try
	{
		bRun = OnStartup();
	}
	__except ( ExceptionHandler( GetExceptionInformation(), m_sThreadName.c_str(), "OnStartup()" ) )
	{
		//LOG( LOG_SYSTEM, "%s: Unhandled exception in OnStartup()", m_sThreadName.c_str() );
	}

	while( bRun )
	{
		__try
		{
			dwResult = MsgWaitForMultipleObjects( m_handles.size(), &m_handles.at(0), FALSE, 
												  m_nWaitTimeout, QS_ALLINPUT );

			if( dwResult >= WAIT_OBJECT_0 && dwResult < WAIT_OBJECT_0 + m_handles.size() )
			{
				if( OnEvent( dwResult - WAIT_OBJECT_0 ) )
				{
					bRun = false;
				}
			}
			else if( dwResult == WAIT_OBJECT_0 + m_handles.size() )
			{
				MSG msg;
				while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
				{
					// process message
					if( OnMsg( &msg ) )
					{
						bRun = false;
						break;
					}
				}
			}
			else if( dwResult == WAIT_TIMEOUT )
			{	
				OnWaitTimeout();
				m_dwLastTimeout = GetTickCount();
			}
			else
			{
#ifdef _DEBUG
				char szMsg[80];
				wsprintf( szMsg, "MsgWaitForMultipleObjects returned %d LastError=%d\n", 
					      dwResult, GetLastError() );
				OutputDebugString( szMsg );
#endif
				bRun = false;
			}

			if( GetTickCount() - m_dwLastTimeout > m_nWaitTimeout )
			{
				OnWaitTimeout();
				m_dwLastTimeout = GetTickCount();
			}

		}
		__except ( ExceptionHandler( GetExceptionInformation(), m_sThreadName.c_str(), "Run()" ) )
		{
			//LOG( LOG_SYSTEM, "%s: Unhandled exception in Run()", m_sThreadName.c_str() );
		}
	}

	__try
	{
		OnShutdown();
	}
	__except ( ExceptionHandler( GetExceptionInformation(), m_sThreadName.c_str(), "OnShutdown()" ) )
	{
		//LOG( LOG_SYSTEM, "%s: Unhandled exception in OnShutdown()", m_sThreadName.c_str() );
//		throw;
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////
// One time initialization
//////////////////////////////////////////////////////////////////////
bool CThread::OnStartup( void )
{
	return true;
}

//////////////////////////////////////////////////////////////////////
// Uninitialize - called before thread exits
//////////////////////////////////////////////////////////////////////
void CThread::OnShutdown( void )
{
}



//////////////////////////////////////////////////////////////////////
// Handle message - return true if WM_QUIT received
//////////////////////////////////////////////////////////////////////
bool CThread::OnMsg( MSG* pMsg )
{
	// handle message
	switch( pMsg->message )
	{
	case WM_QUIT:
		return true;

	default:
		TranslateMessage( pMsg );
		DispatchMessage( pMsg );
		return false;
	}

}

//////////////////////////////////////////////////////////////////////
// Handle event being signalled - return true if Stop Event signaled
//////////////////////////////////////////////////////////////////////
bool CThread::OnEvent( int nIndex )
{
	// handle message
	if( nIndex == 0 )
		return true;

	return false;
}

//////////////////////////////////////////////////////////////////////
// Handle wait timeout - do periodic processing
//////////////////////////////////////////////////////////////////////
bool CThread::OnWaitTimeout( void )
{

	return false;
}

⌨️ 快捷键说明

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