📄 hellothread.c
字号:
/*
* File: HelloWorld.c
* Author: Ray Rischpater (Rocket Mobile, Inc.)
* Purpose:
* Demonstrate the use of IThread
*
*/
#include "AEE.h"
#include "AEEStdLib.h"
#include "AEEAppGen.h" // Applet helper file
#include "helloworld.bid" // Applet-specific header that contains class ID
#include "nmdef.h"
#include "AEEThread.h"
#include "AEEGraphics.h"
typedef struct _SThreadCtx
{
IThread *pit;
int n;
IDisplay *pid;
IShell *pis;
IGraphics *pig;
} SThreadCtx;
typedef struct _CApp
{
AEEApplet a;
SThreadCtx stc1, stc2;
AEECallback cbThread2Done;
int nThread2Result;
} CApp;
/*-------------------------------------------------------------------
Static function prototypes
-------------------------------------------------------------------*/
static void Thread1Start( IThread *pThread, SThreadCtx *pMe );
static void Thread2Start( IThread *pThread, SThreadCtx *pMe );
static void Thread1Main( SThreadCtx *pMe );
static void Thread2Main( SThreadCtx *pMe );
static void Thread2Done( CApp *pMe );
static boolean HelloWorld_HandleEvent(AEEApplet * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj)
{
*ppObj = NULL;
if(AEEApplet_New( sizeof(CApp), // Size of our private class
ClsId, // Our class ID
pIShell, // Shell interface
pMod, // Module instance
(IApplet**)ppObj, // Return object
(AEEHANDLER)HelloWorld_HandleEvent, // Our event handler
NULL)) // No special "cleanup" function
return(AEE_SUCCESS);
return (EFAILED);
}
static void Thread1Start( IThread *pThread, SThreadCtx *pMe )
{
Thread1Main( pMe );
}
static void Thread1Main( SThreadCtx *pMe )
{
AECHAR wsz[ 16 ];
while( 1 )
{
pMe->n++;
WSPRINTF( wsz, sizeof( wsz ), L"t1=%d", pMe->n );
IDISPLAY_DrawText(pMe->pid, // Display instance
AEE_FONT_NORMAL, // Use BOLD font
wsz, // Text - Normally comes from resource
-1, // -1 = Use full string length
0, // Ignored - IDF_ALIGN_CENTER
0, // Ignored - IDF_ALIGN_MIDDLE
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_TOP);
IDISPLAY_Update (pMe->pid);
ISHELL_Resume( pMe->pis, ITHREAD_GetResumeCBK( pMe->pit ) );
ITHREAD_Suspend( pMe->pit );
}
}
static void Thread2Start( IThread *pit, SThreadCtx *pMe )
{
Thread2Main( pMe );
}
static void Thread2Main( SThreadCtx *pMe )
{
AECHAR wsz[ 16 ];
while( pMe->n < 256 )
{
AEECircle circle;
pMe->n++;
circle.cx = 176/2;
circle.cy = 160;
circle.r = pMe->n % 10;
WSPRINTF( wsz, sizeof( wsz ), L"t2=%d", pMe->n );
IDISPLAY_DrawText(pMe->pid, // Display instance
AEE_FONT_NORMAL, // Use BOLD font
wsz, // Text - Normally comes from resource
-1, // -1 = Use full string length
0, // Ignored - IDF_ALIGN_CENTER
0, // Ignored - IDF_ALIGN_MIDDLE
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_BOTTOM);
IDISPLAY_EraseRgn( pMe->pid, 0, 150, 176, 20 );
IGRAPHICS_DrawCircle( pMe->pig, &circle );
IDISPLAY_Update (pMe->pid);
ISHELL_Resume( pMe->pis, ITHREAD_GetResumeCBK( pMe->pit ) );
ITHREAD_Suspend( pMe->pit );
}
ITHREAD_Exit( pMe->pit, SUCCESS );
}
static void Thread2Done( CApp *pMe )
{
DBGPRINTF( pMe->nThread2Result == SUCCESS ? "Thread2 exit SUCCESS" : "Thread2 exit FAILED" );
}
static boolean HelloWorld_HandleEvent(AEEApplet * p, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
AECHAR szText[] = {'H','e','l','l','o',' ','W','o', 'r', 'l', 'd', '\0'};
CApp *pMe = (CApp *)p;
switch (eCode)
{
case EVT_APP_START:
{
int r;
r = ISHELL_CreateInstance( p->m_pIShell, AEECLSID_THREAD, (void **)&pMe->stc1.pit );
r = ISHELL_CreateInstance( p->m_pIShell, AEECLSID_THREAD, (void **)&pMe->stc2.pit );
r = ISHELL_CreateInstance( p->m_pIShell, AEECLSID_GRAPHICS, (void **)&pMe->stc2.pig );
pMe->stc1.pis = p->m_pIShell;
pMe->stc1.pid = p->m_pIDisplay;
pMe->stc2.pis = p->m_pIShell;
pMe->stc2.pid = p->m_pIDisplay;
CALLBACK_Init( &pMe->cbThread2Done, (PFNNOTIFY)Thread2Done, (void *)pMe );
ITHREAD_HoldRsc( pMe->stc2.pit, (IBase *)pMe->stc2.pig );
ITHREAD_Join( pMe->stc2.pit, &pMe->cbThread2Done, &pMe->nThread2Result );
ITHREAD_Start( pMe->stc1.pit, 128, (PFNTHREAD)Thread1Start, (void *)&pMe->stc1 );
ITHREAD_Start( pMe->stc2.pit, 128, (PFNTHREAD)Thread2Start, (void *)&pMe->stc2 );
IDISPLAY_DrawText(p->m_pIDisplay, // Display instance
AEE_FONT_BOLD, // Use BOLD font
szText, // Text - Normally comes from resource
-1, // -1 = Use full string length
0, // Ignored - IDF_ALIGN_CENTER
0, // Ignored - IDF_ALIGN_MIDDLE
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
IDISPLAY_Update (p->m_pIDisplay);
}
return(TRUE);
case EVT_APP_STOP:
{
IGRAPHICS_Release( pMe->stc2.pig );
ITHREAD_Release( pMe->stc1.pit );
ITHREAD_Release( pMe->stc2.pit );
}
return(TRUE);
default:
break;
}
return(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -