Condividi tramite


Determinazione dello stato di avanzamento di un processo

BITS mantiene le informazioni sullo stato di avanzamento per ogni processo. Usare le informazioni sullo stato di avanzamento per determinare il numero di byte e file trasferiti.

Per recuperare informazioni sullo stato di avanzamento per il processo, chiamare il metodo IBackgroundCopyJob::GetProgress , come illustrato nell'esempio seguente. Nell'esempio si presuppone che il puntatore all'interfaccia IBackgroundCopyJob sia valido.

#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); 
}

Per recuperare informazioni sullo stato di avanzamento nella parte di risposta di un processo di risposta di caricamento, chiamare il metodo IBackgroundCopyJob2::GetReplyProgress , come illustrato nell'esempio seguente. Nell'esempio si presuppone che il puntatore all'interfaccia IBackgroundCopyJob sia valido.

#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); 
  }
}

I file contengono anche informazioni sullo stato di avanzamento. Per recuperare le informazioni sullo stato di avanzamento, usare il metodo IBackgroundCopyFile::GetProgress. Per informazioni su come recuperare i file di un processo, vedere Enumerazione di file in un processo.