Interrogation de l’état du travail
Par défaut, une application doit interroger les modifications de l’état d’un travail. Pour capturer les modifications de l’état du travail, appelez la méthode méthode ibackgroundcopyjob :: GetState . Pour capturer les modifications du nombre d’octets et de fichiers transférés, appelez la méthode méthode ibackgroundcopyjob :: GetProgress . Pour récupérer des informations de progression sur la partie réponse d’une tâche de chargement-réponse, appelez la méthode IBackgroundCopyJob2 :: GetReplyProgress . Pour obtenir un exemple qui utilise les informations de progression, consultez détermination de la progression d’un travail.
L’énumération de l' _ _ État du travail BG définit les États d’un travail et la structure de _ _ progression du travail BG contient des informations sur le nombre d’octets et de fichiers transférés.
Pour utiliser l’interrogation, vous devez créer un mécanisme pour lancer l’interrogation. Par exemple, créez un minuteur ou utilisez un bouton « mettre à jour » sur l’interface utilisateur. Toutefois, il peut être plus facile de s’inscrire pour la notification d’événements et de recevoir des événements lorsque l’État ou la progression change. Pour plus d’informations sur la notification d’événements, consultez inscription d’un rappel com.
L’exemple suivant utilise un minuteur pour interroger l’état d’un travail. L’exemple suppose que le pointeur d’interface méthode ibackgroundcopyjob est valide.
HRESULT hr;
IBackgroundCopyJob* pJob;
BG_JOB_STATE State;
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;
//IBackgroundCopyError* pError = NULL;
//BG_JOB_PROGRESS Progress;
//WCHAR *JobStates[] = { L"Queued", L"Connecting", L"Transferring",
// L"Suspended", L"Error", L"Transient Error",
// L"Transferred", L"Acknowledged", L"Canceled"
// };
liDueTime.QuadPart = -10000000; //Poll every 1 second
hTimer = CreateWaitableTimer(NULL, FALSE, L"MyTimer");
SetWaitableTimer(hTimer, &liDueTime, 1000, NULL, NULL, 0);
do
{
WaitForSingleObject(hTimer, INFINITE);
//Use JobStates[State] to set the window text in a user interface.
hr = pJob->GetState(&State);
if (FAILED(hr))
{
//Handle error
}
if (BG_JOB_STATE_TRANSFERRED == State)
//Call pJob->Complete(); to acknowledge that the transfer is complete
//and make the file available to the client.
else if (BG_JOB_STATE_ERROR == State || BG_JOB_STATE_TRANSIENT_ERROR == State)
//Call pJob->GetError(&pError); to retrieve an IBackgroundCopyError interface
//pointer which you use to determine the cause of the error.
else if (BG_JOB_STATE_TRANSFERRING == State)
//Call pJob->GetProgress(&Progress); to determine the number of bytes
//and files transferred.
} while (BG_JOB_STATE_TRANSFERRED != State &&
BG_JOB_STATE_ERROR != State &&
BG_JOB_STATE_TRANSIENT_ERROR != State);
CancelWaitableTimer(hTimer);
CloseHandle(hTimer);