📄 _process.h
字号:
/************************************
REVISION LOG ENTRY
Revision By: Alex Turc
Revised on 6/15/00 12:07:36 PM
Comments: Process related classes
************************************/
#ifndef ___Process_h__
#define ___Process_h__
#include <windows.h>
#include "_exception.h"
#include "_manage.h"
namespace extension
{
/////////////
// Exceptions
/////////////
// Raised when a thread operation failed
class thread_exception :
public extended_exception
{
public:
thread_exception( long nResult, char* pDescription = "Thread exception", char* pFile = NULL, long nLine = 0 ) :
extended_exception( nResult, pDescription, pFile, nLine )
{
}
};
// Raised when a module operation failed
class module_exception :
public extended_exception
{
public:
module_exception( long nResult, char* pDescription = "Module Exception", char* pFile = NULL, long nLine = 0 ) :
extended_exception( nResult, pDescription, pFile, nLine )
{
}
};
/*
09 September 1999
Extend this class whe you wanna have a thread!
*/
class thread
{
public:
// Function name : WINAPI thread_function
// Description : This function is used when creating the win32 thread
// Return type : static DWORD
// Argument : void* pArg
static DWORD WINAPI thread_function(void* pArg)
{
return ((thread*)pArg)->run();
}
// Function name : thread
// Description : Constructor
// Return type :
// Argument : bool bCreateSuspended = true
// Argument : SECURITY_ATTRIBUTES* pSecurityAttributes = NULL
thread( bool bCreateSuspended = true, SECURITY_ATTRIBUTES* pSecurityAttributes = NULL )
throw( thread_exception )
{
m_bTerminateRequest = FALSE;
m_hThread = ::CreateThread( pSecurityAttributes, 0, thread_function, this, ( bCreateSuspended?CREATE_SUSPENDED:0 ) , NULL );
if( m_hThread == NULL )
throw_exception( thread_exception, ::GetLastError(), "Failed to create thread" );
}
// Function name : ~thread
// Description :
// Return type : virtual
virtual ~thread()
{
stop();
}
// Function name : stop
// Description : Stops a thread. Wait for it dwWait seconds
// Return type : virtual void
// Argument : unsigned long dwWait = INFINITE
virtual void stop( unsigned long dwWait = INFINITE )
{
if( m_hThread == NULL )
return;
// Signal that thread should terminate
m_bTerminateRequest = TRUE;
// Wait
if( ::WaitForSingleObject( m_hThread, dwWait ) == WAIT_TIMEOUT )
if( dwWait != INFINITE )
::TerminateThread( m_hThread, 0 );
::CloseHandle( m_hThread );
m_hThread = NULL;
m_bTerminateRequest = FALSE;
}
// Function name : suspend
// Description : Suspends the thread
// Return type : virtual void
virtual void suspend()
{
if( m_hThread )
{
if( ::SuspendThread( m_hThread ) == -1 )
throw_exception( thread_exception, ::GetLastError(), "Failed to suspend thread" );
}
}
// Function name : resume
// Description : Resums a suspended thread
// Return type : virtual void
virtual void resume()
{
if( m_hThread )
{
if( ::ResumeThread( m_hThread ) == -1 )
throw_exception( thread_exception, ::GetLastError(), "Failed to resume thread" );
}
}
// Function name : is_running
// Description : Returns true if the thread is running and false if the thread is not running
// Return type : bool
bool is_running()
{
if( m_hThread == NULL )
return false;
if( ::WaitForSingleObject( m_hThread, 0 ) == WAIT_TIMEOUT )
return true;
return false;
}
// Function name : operator HANDLE
// Description : Returns the handle of the thread
// Return type : HANDLE
operator HANDLE()
{
return m_hThread;
}
// Function name : long run
// Description : This is the thread's method. Because is pure, it must be overided in derived classes.
// Return type : virtual unsigned
virtual unsigned long run() = 0;
// Function name : sleep
// Description : Sleep
// Return type : static
// Argument : unsigned long dwMilliseconds
static sleep( unsigned long dwMilliseconds )
{
::Sleep( dwMilliseconds );
}
protected:
// This is true for a terminate request and false other
bool m_bTerminateRequest;
private:
// Handle of the thread
HANDLE m_hThread;
// Disable some operations on the instances of this classs
thread( const thread& );
thread& operator=( const thread& );
};
} // Namespace extension
#endif // ___Process_h__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -