[More information]

(********************************************************
*  
*  BITS File Download Demo
*  by Jim McKeeth
*  Copyright (c) 2007 by Jim McKeeth
*  http://www.davinciunltd.com/ 
*
*  Available for use under Mozilla Public License 1.1
*  http://www.mozilla.org/MPL/MPL-1.1.html
*
********************************************************)

Program BitsDemo;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils,
  ComObj,
  ActiveX,
  BackgroundCopyManager_TLB;

procedure Work;
var
  bits: IBackgroundCopyManager;
  job: IBackgroundCopyJob;
  jobID: GUID;
  progress: _BG_JOB_PROGRESS;
  state: BG_JOB_STATE;
  again: boolean;
  error: IBackgroundCopyError;
  desc: PWideChar;
  
begin
  bits := CoBackgroundCopyManager_.Create;
  Assert( Succeeded( 
    bits.CreateJob('PNWDUG', BG_JOB_TYPE_DOWNLOAD, jobID, job )
  ));
  
  Assert( Succeeded(
    job.AddFile( 'http://davinciunltd.com/http/green.bmp',
      'C:\Users\dennis\Desktop\pnwdug\green-va-bits.bmp' )
  ));
  
  Assert( Succeeded(
    job.SetPriority( BG_JOB_PRIORITY_HIGH )
  ));
  
  again := false;
  repeat

{$REGION 'Case to process state'}
    Assert( Succeeded( job.GetState( state )));
    case state of 
      // Continue states
      BG_JOB_STATE_QUEUED: begin
        Writeln('QUEUED');  		again := true;
      end;
      BG_JOB_STATE_CONNECTING: begin
        Writeln('CONNECTING');		again := true;
      end;
      BG_JOB_STATE_TRANSFERRING: begin
        Writeln('TRANSFERRING');	again := true;
      end;
      BG_JOB_STATE_TRANSFERRED: begin
        Writeln('TRANSFERRED . . . completing. . . ');again := true;
        Assert( succeeded( job.Complete() ));        
      end;
      BG_JOB_STATE_ACKNOWLEDGED: begin
        Writeln('Complete ACKNOWLEDGED');again := false;
      end;
      BG_JOB_STATE_SUSPENDED: begin
        Writeln('SUSPENDED');		again := true;
        // Just started, or suspended
        Assert( Succeeded( job.Resume() ));
      end;
      
      // finished states
      BG_JOB_STATE_CANCELLED: begin
        Writeln('CANCELLED');		again := false;
      end;
      BG_JOB_STATE_ERROR, BG_JOB_STATE_TRANSIENT_ERROR: begin
        Writeln('Error:');
        Assert( Succeeded( job.GetError( error )));
        Assert( Succeeded( error.GetErrorDescription( LANG_NEUTRAL, desc )));
        Writeln('Description: ' + string( desc ));
        Assert( Succeeded( error.GetErrorContextDescription( LANG_NEUTRAL, desc )));
        Writeln('Context: ' + string( desc ));
        Assert( Succeeded( error.GetProtocol( desc )));
        Writeln('Protocol: ' + string( desc ));       
        CoTaskMemFree( desc );
        Writeln( 'Canceling. . . . ' );
        Assert( Succeeded( job.Cancel() ));
        again := false;
      end else begin
        Writeln( 'Unknown state!' );
        Assert( Succeeded( job.Cancel() ));
        again := false;
      end;
    end;
{$ENDREGION}
    
    Assert( Succeeded(
      job.GetProgress( progress )
    ));
    
    Writeln( format( '%d bytes transfered of %d bytes total.',
      [progress.BytesTransferred, progress.BytesTotal] ));

    if again then
      sleep( 1000 );
      
  until not again;
end;

begin
  Assert( Succeeded( CoInitialize( nil )));
  try
    try
      Work();
    except
      on E: Exception do
        Writeln( E.Classname, ': ', E.Message );
    end;
  finally
    CoUninitialize;
  end;
  Writeln( 'Done.' );
  Readln;
end.