轮询作业的状态
默认情况下,应用程序必须轮询作业状态的更改。 若要捕获作业状态的更改,请调用 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);