Question

I am using SharpBITS (http://sharpbits.codeplex.com/), a well running BITS wrapper for .NET. I am not quite sure but reckon that BITS must run in a Single Thread Apartment (STA). With older versions I had some problems when running BITS in a Multi Threaded Apartment (MTA) which is for example default for .NET console applications. I do not find anything about that in the BITS documentation or on the Internet.

Even SharpBITS tries to set the apartment state in the constructor of BitsManager to STA (what actually would only be possible before a thread is started).

Does someone know if BITS really must run in an STA? The currrent version seems to run as well in a MTA.

Was it helpful?

Solution

No. The various versions of the BackgroundCopyManager coclass are registered in the registry with the ThreadingModel key set to "Both". Which means that you can create one in an MTA thread (default for any Thread you create yourself or a console app's main thread) and calls will not be marshaled. And if you create one in an STA thread then calls will be marshaled back to that thread.

And yes, MTA can create problems because now it is up to you to ensure that your own code is thread-safe. There are a lot of guarantees you get from an STA thread, you in effect borrow the thread-safety that COM provides. No such guarantees from an MTA, you have to make sure your code is as thread-safe as BITS is. The notifications you'll get from BITS would be the typical troublemaker, locking is required to protect your own state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top