處理錯誤 (BITS)

應用程式中有兩種類型的錯誤要處理。 第一個錯誤是失敗的方法呼叫。 每個方法都會傳 回 HRESULT 值。 每個方法的參考頁面都會識別最有可能產生的傳回值。 如需其他傳回值,請參閱 BITS 傳回值。 若要取得與傳回值相關聯的消息正文,請呼叫 IBackgroundCopyManager::GetErrorDescription 方法。

要處理的第二種錯誤類型是工作,其 狀態 會轉換成 BG_JOB_STATE_ERRORBG_JOB_STATE_TRANSIENT_ERROR。 若要擷取這些錯誤類型的相關信息,請呼叫作業的 IBackgroundCopyJob::GetError 方法。 方法會傳 回IBackgroundCopyError 介面指標,其中包含您用來判斷錯誤原因的資訊。 您也可以註冊以接收事件通知來接收錯誤通知。 如需詳細資訊,請參閱 註冊 COM 回呼

BITS 會將每個作業視為不可部分完成。 如果作業中的其中一個檔案產生錯誤,工作會維持在錯誤狀態,直到解決錯誤為止。 因此,您無法從作業中刪除造成錯誤的檔案。 不過,如果錯誤是由伺服器無法使用或無效的遠端檔案所造成,您可以呼叫 IBackgroundCopyJob3::ReplaceRemotePrefix IBackgroundCopyFile2::SetRemoteName 方法來識別新的伺服器或檔名。

判斷錯誤的原因之後,請執行下列其中一個選項:

針對上傳回復作業,請檢查BG_JOB_REPLY_PROGRESS結構的 BytesTotal 成員,以判斷作業上傳或回復部分是否發生錯誤。 如果值BG_SIZE_UNKNOWN,則上傳時發生錯誤。

下列範例示範如何擷 取 IBackgroundCopyError 介面指標。 此範例假設 IBackgroundCopyJob 介面指標有效。

HRESULT hr = 0;
HRESULT hrError = 0;
IBackgroundCopyJob* pJob;
IBackgroundCopyError* pError = NULL;
IBackgroundCopyFile* pFile = NULL;
WCHAR* pszDescription = NULL;
WCHAR* pszRemoteName = NULL;
BG_ERROR_CONTEXT Context;

hr = pJob->GetError(&pError);
if (SUCCEEDED(hr))
{
  //Retrieve the HRESULT associated with the error. The context tells you
  //where the error occurred, for example, in the transport, queue manager, the 
  //local file, or the remote file.
  pError->GetError(&Context, &hrError);  

  //Retrieve a description associated with the HRESULT value.
  hr = pError->GetErrorDescription(LANGIDFROMLCID(GetThreadLocale()), &pszDescription);
  if (SUCCEEDED(hr))
  {
    if (BG_ERROR_CONTEXT_REMOTE_FILE == Context)
    {
      hr = pError->GetFile(&pFile);  
      if (SUCCEEDED(hr))
      {
        hr = pFile->GetRemoteName(&pszRemoteName);
        if (SUCCEEDED(hr))
        {
          //Do something with the information.
          CoTaskMemFree(pszRemoteName);
        }
        pFile->Release();
      }
    }
    CoTaskMemFree(pszDescription);
  }
  pError->Release();
}
else
{
  //Error information is not available.
}