輪詢作業的狀態
根據預設,應用程式必須輪詢作業狀態的變更。 若要擷取作業狀態中的變更,請呼叫 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);