Condividi tramite


Polling per lo stato del processo

Per impostazione predefinita, un'applicazione deve eseguire il polling delle modifiche apportate allo stato di un processo. Per acquisire le modifiche nello stato del processo, chiamare il metodo IBackgroundCopyJob::GetState . Per acquisire le modifiche nel numero di byte e file trasferiti, chiamare il metodo IBackgroundCopyJob::GetProgress. Per recuperare informazioni sullo stato di avanzamento nella parte di risposta di un processo upload-reply, chiamare il metodo IBackgroundCopyJob2::GetReplyProgress. Per un esempio che usa le informazioni sullo stato di avanzamento, vedere Determinazione dello stato di avanzamento di un processo.

L'enumerazione BG_JOB_STATE definisce gli stati di un processo e la struttura BG_JOB_PROGRESS contiene informazioni sul numero di byte e file trasferiti.

Per usare il polling, è necessario creare un meccanismo per avviare il polling. Ad esempio, creare un timer o usare un pulsante "Aggiorna" nell'interfaccia utente. Tuttavia, potrebbe essere più facile registrarsi per la notifica degli eventi e ricevere eventi quando lo stato o lo stato cambia. Per informazioni sulla notifica degli eventi, vedere Registrazione di un callback COM.

Nell'esempio seguente viene utilizzato un timer per eseguire il polling dello stato di un processo. Nell'esempio si presuppone che il puntatore all'interfaccia IBackgroundCopyJob sia valido.

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);