home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 13
/
mediashare_13.zip
/
mediashare_13
/
ZIPPED
/
PROGRAM
/
WTJ9403.ZIP
/
WILDASS
/
SOURCE
/
TASKS.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-12
|
7KB
|
376 lines
// tasks.cpp
//
// implementation of the CTask
// and CTaskList classes
//
// update: 1.00 06-08-93 tw
#include <afxwin.h>
#include <afxext.h>
#include <ctl3d.h>
#include "tasks.h"
#define new DEBUG_NEW
// ***************** task class
CTask::CTask( TASKENTRY * pte )
{
// set the size member to the appropriate size
// and copy the taskentry info
memcpy( &m_te, pte, sizeof( TASKENTRY ));
// get the Name of the .exe file that
// is stored in the moduleentray for this
// task
MODULEENTRY me;
me.dwSize = sizeof( MODULEENTRY );
VERIFY( ModuleFindHandle( &me, HModule() ));
m_strExePath = me.szExePath;
}
HTASK
CTask::HTask() const
{
// May return invalid task handles
#ifdef _DEBUG
if ( ! ::IsTask( m_te.hTask ))
{
TRACE("Warning:CTask::HTask() returning invalid TASKENTRY\n"
"Dumping Task:\n");
Dump();
}
#endif
return m_te.hTask;
}
CTask::operator HTASK() const
{
// May return invalid task handles
#ifdef _DEBUG
if ( ! ::IsTask( m_te.hTask ))
{
TRACE("Warning:CTask::operator HTASK() returning invalid TASKENTRY\n"
"Dumping Task:\n");
Dump();
}
#endif
return m_te.hTask;
}
HINSTANCE
CTask::HInstance() const
{
ASSERT_VALID( this );
return m_te.hInst;
}
CTask::operator HINSTANCE() const
{
ASSERT_VALID( this );
return m_te.hInst;
}
HMODULE
CTask::HModule() const
{
ASSERT_VALID( this );
return m_te.hModule;
}
// wont work as HINSTANCE==HMODULE
//CTask::operator HMODULE() const
//{
// ASSERT_VALID( this );
// return m_te.hModule;
//}
const char*
CTask::ExeName() const
{
ASSERT_VALID( this );
return m_strExePath;
}
const char*
CTask::ModuleName() const
{
ASSERT_VALID( this );
return m_te.szModule;
}
// not so clean task termination
void
CTask::DieHard() const
{
ASSERT_VALID( this );
TerminateApp( HTask(), NO_UAE_BOX );
}
// do a clean termination of this task
void
CTask::Die() const
{
ASSERT_VALID( this );
VERIFY( EnumTaskWindows( HTask(),
EnumTaskWndProcCloseApp,
0 ));
}
/* static */
// this fxn is called for all non-owned top-level window
// of this task
BOOL CALLBACK __export
CTask::EnumTaskWndProcCloseApp( HWND hWnd, LPARAM /*lParam*/ )
{
CWnd * pWnd = CWnd::FromHandle( hWnd );
if ( pWnd->IsWindowEnabled() )
{
pWnd->PostMessage( WM_CLOSE );
}
return TRUE;
}
/* static */
BOOL
CTask::IsTask( CTask * pTask )
{
return ::IsTask( pTask->HTask() );
}
/* static */
BOOL
CTask::IsTask( HTASK task )
{
return ::IsTask( task );
}
BOOL
CTask::IsTask() const
{
return ::IsTask( m_te.hTask );
}
#ifdef _DEBUG
void
CTask::Dump() const
{
afxDump << "[CTask] Module: " << ModuleName() << " .exe: " << ExeName() << "\n";
}
void
CTask::AssertValid() const
{
ASSERT( ::IsTask( m_te.hTask ));
}
#endif
HTASK
CTask::HParent() const
{
ASSERT_VALID( this );
return m_te.hTaskParent;
}
WORD
CTask::Events() const
{
ASSERT_VALID( this );
return m_te.wcEvents;
}
WORD
CTask::Usage() const
{
ASSERT_VALID( this );
MODULEENTRY me;
me.dwSize = sizeof( MODULEENTRY );
VERIFY( ModuleFindHandle( &me, HModule() ));
return me.wcUsage;
}
// **********************[list of tasks]
CTaskList::CTaskList()
{
// contructing a tasklist means rebuilding the list
Rebuild();
}
CTaskList::~CTaskList()
{
// destucting the list involves clearing the list
Clear();
}
void
CTaskList::Rebuild()
{
// cleat the list
Clear();
// get a taskentry and initialize the size member
TASKENTRY te;
te.dwSize = sizeof( TASKENTRY );
// get the first task from the task queue
VERIFY( TaskFirst( &te ));
// add it to the list
AddTail( new CTask( &te ));
// get the other tasks from the task queue
// and add them to the list
while ( TaskNext( &te ))
{
AddTail( new CTask( &te ));
}
#ifdef _DEBUG
Dump();
#endif
}
void
CTaskList::Clear()
{
// release the list and all its members
for( POSITION pos = GetHeadPosition(); pos != NULL; )
{
delete GetNext( pos );
}
RemoveAll();
}
void
CTaskList::CloseDirty( CTask * pTask )
{
POSITION pos = Find( pTask );
// must be in here... no one else
// allowed to remove tasks from us
ASSERT( pos );
// make sure this task still has a valid taskhandle
// if not, only remove the CTask from list, but
// _dont_ kill the task
if ( pTask->IsTask() )
{
#ifdef _DEBUG
TRACE("Dirty termination of Task - Dumping Task:\n");
pTask->Dump();
#endif
pTask->DieHard();
}
// remove the CTask from the list
RemoveAt( pos );
// free the associated memory
delete pTask;
#ifdef _DEBUG
Dump();
#endif
}
void
CTaskList::CloseClean( CTask * pTask )
{
POSITION pos = Find( pTask );
// must be in here... no one else
// allowed to remove tasks from us
ASSERT( pos );
// make sure this is still a valid task
if ( pTask->IsTask() )
{
#ifdef _DEBUG
TRACE("Clean termination of Task - Dumping Task:\n");
pTask->Dump();
#endif
pTask->Die();
}
#ifdef _DEBUG
else
{
TRACE("Warning: trying to close an invalid task in CTaskList::CloseClean\n");
}
#endif
// remove from list
RemoveAt( pos );
// free the associated memory
delete pTask;
#ifdef _DEBUG
Dump();
#endif
}
// avoid casts in usage
CTask*&
CTaskList::GetNext( POSITION& rPos )
{
return (CTask*&) CObList::GetNext(rPos);
}
CTask*
CTaskList::GetNext( POSITION& rPos ) const
{
return (CTask*) CObList::GetNext(rPos);
}
CTask*&
CTaskList::GetAt( POSITION pos )
{
return (CTask*&) CObList::GetAt( pos );
}
CTask*
CTaskList::GetAt( POSITION pos ) const
{
return (CTask*) CObList::GetAt( pos );
}
void
CTaskList::Remove( CTask * pTask )
{
POSITION pos = Find( pTask );
// must be in here... no one else
// allowed to remove tasks from us
ASSERT( pos );
RemoveAt( pos );
delete pTask;
}
#ifdef _DEBUG
void
CTaskList::Dump() const
{
afxDump << "Dumping a CTaskList with " << GetCount() << " items\n";
for( POSITION pos = GetHeadPosition(); pos != NULL; )
{
CTask * pTask = GetNext( pos );
pTask->Dump();
}
}
#endif