Tutorial Asynchronous
Tutorial Asynchronous
Tutorial – Asynchronous
Loading Demo
Foxit PDF DLL SDK
Asynchronous Loading Demo
Contents
Prerequisites ............................................................................................................... 3
Developer Audience ............................................................................................. 3
Supported Environments ...................................................................................... 3
Overview .................................................................................................................... 3
Purpose ............................................................................................................... 3
Setup ......................................................................................................................... 3
Demo Functionalities ................................................................................................... 4
Design ................................................................................................................. 4
Thread .......................................................................................................... 5
Timer ............................................................................................................ 7
2
Foxit PDF DLL SDK
Asynchronous Loading Demo
Prerequisites
Developer Audience
This document is targeted towards C/C++ developers using the Foxit PDF DLL SDK. It
assumes the developer is familiar with C/C++ and Microsoft Foundation Classes (MFC).
Supported Environments
Overview
Purpose
This document covers how to use the Foxit PDF DLL SDK’s Asynchronous file loading
mechanism. It uses the demo provided by Foxit Corporation as reference for explanation.
Setup
1) Download these two items from Foxit:
a. Evaluation version of the Foxit PDF DLL SDK
b. The Visual C++ 6.0 Demo
2) Extract both .zip files. Move fpdfsdk.dll and fpfsdk.lib to <yourDemoDirectory>/bin
3) Copy <yourSdkDirectory>/includes/*.h to <yourDemoDirectory>/includes,
overwriting all .h’s. You may encounter errors in building the demo if this is
not done
4) Open examples.dsw with Visual Studio 6.0
5) Compile “Asynchronous” project demo
3
Foxit PDF DLL SDK
Asynchronous Loading Demo
Demo Functionalities
The following sections contain references to the “Asynchronous” demo.
Design
The demo uses MFC’s threading capability and timer capability to implement asynchronous
loading. It also uses Critical Section capability of MFC to protect field accessed by both
thread; namely, theApp.m_workSignal.
void CAsynchronousDlg::OnBtnOpenclose()
{
if (m_bOpen)
{
//... set speed of loading
//... set file path
//... set data manager for the file
//... set Foxit bitmap for use
//update signal
EnterCriticalSection(&theApp.m_WorkSignalLock);
theApp.m_WorkSignal = SIGNAL_REQ_DOC;
LeaveCriticalSection(&theApp.m_WorkSignalLock);
//update signal
EnterCriticalSection( &theApp.m_WorkSignalLock );
theApp.m_WorkSignal = SIGNAL_REQ_EXIT_THREAD;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
4
Foxit PDF DLL SDK
Asynchronous Loading Demo
//... cleanup
}
}
Thread
The thread consumes the signal passed to it by the timer or by the initialization (see
above). Use FPDFAvail_Create() to initialize an availability handler. Use
FPDFAvail_IsDocAvail() to check if document is available. Use FPDFAvail_GetDocument()
to get available doc (after it finishes loading), and use FPDFAvail_isPageAvail() to check
if the page given is loaded and available.
static UINT operateThread( LPVOID pParam )
{
int bFlagThreadOver = FALSE;
while( bFlagThreadOver == FALSE )
{
//read the signal
EnterCriticalSection( &theApp.m_WorkSignalLock );
iWorkSignal = theApp.m_WorkSignal;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
switch ( iWorkSignal )
{
case SIGNAL_STATE_IDLE:
{
Sleep( 0);
break;
}
case SIGNAL_REQ_DOC://requesting a new doc
{
//...
g_Avail = FPDFAvail_Create( &g_Fileavail, &g_Fileaccess );
int nTemp ;
nTemp = FPDFAvail_IsDocAvail( g_Avail,&g_DownloadHits );
//load doc in while loop
while ( !nTemp )
{
//perform opening doc task with the data manager associated
//with the doc
theApp.m_DataMgrapp->ExcuteTask();
if(g_Avail)
{
//check if doc is still available after task is performed
5
Foxit PDF DLL SDK
Asynchronous Loading Demo
nTemp =
FPDFAvail_IsDocAvail( g_Avail,&g_DownloadHits );
}
//...
}
if( theApp.m_pDoc )
FPDF_CloseDocument( theApp.m_pDoc );
//all doc loaded at this point, use FPDFAvail_GetDocument to get
//pointer to FPDF_DOCUMENT object that is fully loaded
theApp.m_pDoc = FPDFAvail_GetDocument(g_Avail);
theApp.m_iPageCount = FPDF_GetPageCount( theApp.m_pDoc );
theApp.m_iPageIndex = 0;
double dbWidth = 0;
double dbHeight = 0;
int ret = FPDF_GetPageSizeByIndex( theApp.m_pDoc, 0, &dbWidth,
&dbHeight );
//update signal
EnterCriticalSection( &theApp.m_WorkSignalLock );
theApp.m_WorkSignal = SIGNAL_DOC_READY;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
break;
}
case SIGNAL_REQ_PAGE:
{
int nTmp;
nTmp = FPDFAvail_IsPageAvail(
g_Avail, theApp.m_iPageIndex, &g_DownloadHits );
theApp.m_DataMgrapp->ExcuteTask();
//load page in while loop
while ( !nTmp )
{
if (g_Avail)
{
nTmp = FPDFAvail_IsPageAvail( g_Avail,
theApp.m_iPageIndex, &g_DownloadHits ) ;
theApp.m_DataMgrapp->ExcuteTask();
}
//...
if ( theApp.m_pPage )
FPDF_ClosePage( theApp.m_pPage );
//page is loaded, now grab the page pointer
6
Foxit PDF DLL SDK
Asynchronous Loading Demo
theApp.m_pPage =
FPDF_LoadPage( theApp.m_pDoc, theApp.m_iPageIndex );
//update signal
EnterCriticalSection( &theApp.m_WorkSignalLock );
theApp.m_WorkSignal = SIGNAL_PAGE_READY;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
break;
}
case SIGNAL_REQ_EXIT_THREAD:
{
//update signal
bFlagThreadOver = TRUE;
EnterCriticalSection( &theApp.m_WorkSignalLock );
theApp.m_WorkSignal = SIGNAL_STATE_IDLE;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
break;
}
case SIGNAL_DOC_READY:
case SIGNAL_PAGE_READY:
{
Sleep( 50 );
break;
}
}
}
g_pDlg->m_hThread = NULL;
return 0;
}
Timer
Timer checks the signal periodically to update progress and signal next step for thread
void CAsynchronousDlg::OnTimer(UINT nIDEvent)
{
//check the signal
int iWorkSignal = 0;
EnterCriticalSection( &theApp.m_WorkSignalLock );
iWorkSignal = theApp.m_WorkSignal;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
switch ( iWorkSignal )
{
7
Foxit PDF DLL SDK
Asynchronous Loading Demo
case SIGNAL_DOC_READY:
{
//doc is ready, signal thread to request for page
EnterCriticalSection( &theApp.m_WorkSignalLock );
theApp.m_WorkSignal = SIGNAL_REQ_PAGE;
LeaveCriticalSection( &theApp.m_WorkSignalLock );
//... prepare the page bitmap with the width and height information
// according to you device context