|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere are a few articles about the Background Intelligent Transfer Service or BITS here. I wanted to add something more. Using BITS with COM interop isn't really difficult, but I thought this would be a fun project to do anyway. For a complete reference on BITS, do read all about it on MSDN first. When I started, I had some goals in mind.
Here's a simple example of creating a download job. System.Net.BITS.Manager manager = new System.Net.BITS.Manager();
System.Net.BITS.Job job = new System.Net.BITS.Job("Simple Job");
job.Files.Add("http://www.someplace.com/download/file.zip", "c:\file.zip");
manager.Jobs.Add(job);
job.Resume();
That's pretty easy. You could get away with doing this little. BITS will take care of the download, and What this example lacks is a way to know when the job finishes. You can do this in one of two ways, using polling or events. Polling works just fine, use a timer to check the job state, or if you need something synchronous a simple loop will work. while (job.State != JobState.Cancelled &&
job.State != JobState.Acknowledged)
Sleep(1000);
// we're done! Do Something
Of course, polling is not the best approach. The job will eventually finish one way or another, but the loop above could take a while. Let's use events instead. System.Net.BITS.Manager manager = new System.Net.BITS.Manager();
System.Net.BITS.Job job = new System.Net.BITS.Job("Simple Job");
manager.OnModfication += new
EventHandler<JobModificationEventArgs>(manager_OnModfication);
job.Files.Add("http://www.someplace.com/download/file.zip",
"c:\file.zip");
manager.Jobs.Add(job);
job.Resume();
}
private void manager_OnModfication(object sender,
JobModificationEventArgs e)
{
if (e.Job.State == JobState.Cancelled &&
e.Job.State == JobState.Acknowledged)
{
// we're done! Do Something
}
}
There's a lot more to BITS that what is in this example. Fortunately for me, it's documented by Microsoft already. I did add a few things, so I'll go over them. Not everything will be covered, just the important stuff. GeneralEach version of BITS adds more features. Be sure to check what version of BITS was instantiated. An exception will be thrown if you try to use features from a higher version of BITS than what you have on your machine. You won't get the exception in the designer. You can set everything in the designer, you'll get the exception as soon as your code runs. Be careful not to set properties in the designer if you want to support older versions of BITS. I didn't test everything! Sorry. I tested a lot of the functionality on both XP and Vista. I did not try an upload with reply job. Not everything works as Microsoft has documented. When running on XP, if you set a command line notification, it will not run unless you set The documentation on MSDN is not up to date, and some of the stuff added in BITS version 3.0 is not documented. If you have the The Manager ClassYou will need to create an instance of The Jobs can be added or removed from the collection at any time. When removed from the collection, the The BITS likes to send lots of events and on different threads. BITS doesn't queue up events for the same job. You can have multiple events pre-empting each other. I think that's annoying. So I wrapped all the events in a Mutex. The Job ClassFor the most part, this is just a wrapper around Jobs can be cloned. All the job properties are set on the clone. Even the credentials. This will only work if you set all the credentials in this job instance. There's no way to get them from the Jobs maintain a collection of files. While designing a job, you can add and removes files as you wish. Once the Some properties, like job.ProxySettings.ProxyUsage = ProxyUsage.NoProxy;
It won't do anything. I could have made that work but decided against it. JobProxySettings settings = job.ProxySettings;
settings.ProxyUsage = ProxyUsage.NoProxy;
job.ProxySettings.ProxyUsage = settings;
Doing the above will result in a call to ReferencesHistory
|
||||||||||||||||||||||