Using SharpBITS to transfer (download) file

1. Add a reference to the SharpBITS.dll on your project (download from SharpBITS.NET).
2. Add multi-line TextBox named “output” to your form.
3. Add following piece of code to your form code:

public partial class BITStester : Form
{

BitsManager manager = null;
object jobTransferredSync = new object();
object jobFailedSync = new object();
const string fromURL = @"http://localhost/download/test.txt";
const string fileTo = @"c:/temp/test.txt";

private void BITStester_Load(object sender, EventArgs e)
{
	manager = new BitsManager();
	manager.OnJobTransferred += new EventHandler(OnJobTransferred);
	manager.OnJobError += new EventHandler(OnJobError);
	manager.EnumJobs(JobOwner.AllUsers);	

	FileInfo file = new FileInfo(fileTo);

	BitsJob job = manager.CreateJob(file.Name, JobType.Download);

	job.NotificationFlags = NotificationFlags.JobTransferred;

	// The default retry delay was 600 seconds (10 minutes).
	job.MinimumRetryDelay = 60; // 1 min
	job.Priority = JobPriority.High;
	job.Description = "Created by BITStester";
	job.AddFile(fromURL, fileTo);
	job.Resume();

	AddToOutput("File {0} added to BITS pool", file.Name);
}

private void OnJobTransferred(object sender, NotificationEventArgs e)
{
	lock (jobTransferredSync)
	{
		BitsJob job = e.Job;
		job.Complete();
		AddToOutputHelper(string.Format("File {0} downloaded", job.DisplayName));
	}
}

private void OnJobError(object sender, NotificationEventArgs e)
{
	lock (jobFailedSync)
	{
		BitsJob job = e.Job;
		string errorMessage = string.IsNullOrEmpty(job.Description) ? 
				"Error description not provided" : job.Error.Description;

		AddToOutputHelper(string.Format("File download {0} failed: {1}", 
				job.DisplayName, errorMessage));
		job.Cancel();
	}
}

#region Output

private delegate void UpdateOutputHandler(string line);

// To call also from another thread
private void AddToOutputHelper(string line)
{
	if (output.InvokeRequired)
	{
		output.Invoke(new UpdateOutputHandler(AddToOutput), line);
	}
	else
	{
		AddToOutput(line);
	}
}

private void AddToOutput(string line)
{
	output.AppendText(line + Environment.NewLine);
}

private void AddToOutput(string line, params object[] param)
{
	output.AppendText(string.Format(line, param) + Environment.NewLine);
}

#endregion Output
} // class

 

Useful links when using BITS technology:

Posted in BITS, C#. Tags: , , , , . Leave a Comment »

How to Run PHP on Vista

After googling I found few useful links:

Great – I got running PHP on Vista Ultimate along ASPX in single Web Site by configuring as FastCGI module.

About second option (ISAPI). PHP 5 doesn’t contain such php5isapi.dll described in configuration. I suppose that it’s mentioned only for older versions of PHP.