Share via


列舉傳輸佇列中的作業

若要從傳輸佇列列舉作業,請呼叫 IBackgroundCopyManager::EnumJobs 方法。 方法會 傳回您用來列舉佇列中作業的 IEnumBackgroundCopyJobs 介面指標。

若要擷取使用者的工作,請將 EnumJobs 方法的第一個參數設定為 0。 若要擷取佇列中的所有作業,請將 EnumJobs 方法的第一個參數設定為BG_JOB_ENUM_ALL_USERS。 只有具有系統管理員許可權的使用者才能擷取傳輸佇列中的所有作業。

請注意,列舉清單是您呼叫 EnumJobs 方法時,傳輸佇列中作業的快照集。 不過,這些作業的屬性值會反映作業的目前值。

如果您想要擷取個別傳輸作業,請呼叫 IBackgroundCopyManager::GetJob 方法。

若要列舉作業中的檔案,請參閱 列舉作業中的檔案。

下列範例示範如何列舉傳輸佇列中的作業。 範例中的g_XferManager變數是 IBackgroundCopyManager 介面指標。 如需如何建立 IBackgroundCopyManager 介面指標的詳細資訊,請參閱 連線 BITS 服務

HRESULT hr = 0;
IEnumBackgroundCopyJobs* pJobs = NULL;
IBackgroundCopyJob* pJob = NULL;
ULONG cJobCount = 0;
ULONG idx = 0;

//This example enumerates all jobs in the transfer queue. This call fails if the 
//current user does not have administrator privileges. To enumerate jobs for only 
//the current user, replace BG_JOB_ENUM_ALL_USERS with 0.
hr = g_XferManager->EnumJobs(BG_JOB_ENUM_ALL_USERS, &pJobs);
if (SUCCEEDED(hr))
{
  //Get the count of jobs in the queue. 
  pJobs->GetCount(&cJobCount);

  //Enumerate the jobs in the queue.
  for (idx=0; idx<cJobCount; idx++)
  {
    hr = pJobs->Next(1, &pJob, NULL);
    if (S_OK == hr)
    {
      //Retrieve or set job properties.

      pJob->Release();
      pJob = NULL;
    }
    else
    {
      //Handle error
      break;
    }
  }

  pJobs->Release();
  pJobs = NULL;
}