다음을 통해 공유


작업 상태에 대한 폴링

기본적으로 애플리케이션은 작업 상태 변경 내용을 폴링해야 합니다. 작업 상태의 변경 내용을 캡처하려면 IBackgroundCopyJob::GetState 메서드를 호출합니다. 전송된 바이트 및 파일 수의 변경 내용을 캡처하려면 IBackgroundCopyJob::GetProgress 메서드를 호출합니다. 업로드-회신 작업의 회신 부분에 대한 진행률 정보를 검색하려면 IBackgroundCopyJob2::GetReplyProgress 메서드를 호출합니다. 진행률 정보를 사용하는 예제는 작업 진행률 확인 을 참조하세요.

BG_JOB_STATE 열거형은 작업의 상태를 정의하고 BG_JOB_PROGRESS 구조에는 전송된 바이트 및 파일 수에 대한 정보가 포함됩니다.

폴링을 사용하려면 폴링을 시작하는 메커니즘을 만들어야 합니다. 예를 들어 타이머를 만들거나 사용자 인터페이스에서 "업데이트" 단추를 사용합니다. 그러나 상태 또는 진행률이 변경될 때 이벤트 알림을 등록하고 이벤트를 수신하는 것이 더 쉬울 수 있습니다. 이벤트 알림에 대한 자세한 내용은 COM 콜백 등록을 참조하세요.

다음 예제에서는 타이머를 사용하여 작업 상태를 폴링합니다. 이 예제에서는 IBackgroundCopyJob 인터페이스 포인터가 유효하다고 가정합니다.

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