确定作业的进度
BITS 维护每个作业的进度信息。 使用进度信息来确定传输了多少个字节和文件。
若要检索作业的进度信息,请调用 IBackgroundCopyJob::GetProgress 方法,如以下示例所示。 该示例假定 IBackgroundCopyJob 接口指针有效。
#define PROGRESS_COMPLETE_LEN 50
HRESULT hr;
IBackgroundCopyJob* pJob;
WCHAR szProgressComplete[PROGRESS_COMPLETE_LEN+1];
BG_JOB_PROGRESS Progress;
hr = pJob->GetProgress(&Progress);
if (FAILED(hr))
{
//Handle error
}
//Because the BytesTotal member can be 0 or BG_SIZE_UNKNOWN, you may not be able
//to determine a percentage value to display, such as 57%. It is best to display a
//string that shows the number of bytes transferred. For example, "123456 of
//999999" or "123456 of Unknown".
if (BG_SIZE_UNKNOWN == Progress.BytesTotal)
{
StringCchPrintf(szProgressComplete, PROGRESS_COMPLETE_LEN+1, L"%I64d of Unknown",
Progress.BytesTransferred);
}
else
{
StringCchPrintf(szProgressComplete, PROGRESS_COMPLETE_LEN+1, L"%I64d of %I64d",
Progress.BytesTransferred, Progress.BytesTotal);
}
若要检索有关上传答复作业的答复部分的进度信息,请调用 IBackgroundCopyJob2::GetReplyProgress 方法,如以下示例所示。 该示例假定 IBackgroundCopyJob 接口指针有效。
#define REPLY_COMPLETE_LEN 4
HRESULT hr;
IBackgroundCopyJob* pJob;
IBackgroundCopyJob2* pJob2 = NULL;
WCHAR szReplyComplete[REPLY_COMPLETE_LEN+1];
BG_JOB_REPLY_PROGRESS Progress;
pJob->QueryInterface(__uuidof(IBackgroundCopyJob2), (void**)&pJob2);
hr = pJob2->GetReplyProgress(&Progress);
if (SUCCEEDED(hr))
{
if (0 == Progress.BytesTotal) //The job type is not BG_JOB_TYPE_UPLOAD_REPLY
{
//Logic to deal with this case
}
else if (BG_SIZE_UNKNOWN == Progress.BytesTotal) //The reply has not begun
{
StringCchPrintf(szReplyComplete, REPLY_COMPLETE_LEN+1, L"0%%");
}
else
{
StringCchPrintf(szReplyComplete, REPLY_COMPLETE_LEN+1 L"%I64d%%",
100*Progress.BytesTransferred/Progress.BytesTotal);
}
}
文件还包含进度信息。 若要检索进度信息,请使用 IBackgroundCopyFile::GetProgress 方法。 有关如何检索作业文件的信息,请参阅枚举作业中的文件。