This project has moved. For the latest updates, please go here.

MEF ThreadSafe

Dec 8, 2010 at 10:16 AM


I'am a new user of MEF and I am testing it. I have few questions about Thread Safe and the LifeTime of Parts.

I Try to create a multi-threaded application for processing images. All process will be created as Parts.

I have create a class named UnitCore. This class have an ImportMany of IEnumerable<Lazy<IProcess, IProcessData>>.

The UnitCore Class also have a Thread. The ThreadStart function correspond to the IProcess.Process() function in the list that i need.

In order to avoid thread working on the same instance of Parts I use NonShared Parts. This allow me to call the same Process in different thread simultaneously.

I have a main Thread that create a DirectoryCatalog, a CompositionContainer and 6 UnitCore Instance.

After Instantiate the UnitCores, i call the container.ComposeParts method for each UnitCore with its reference in param.

After this i call the UnitCore.Start() method for each UnitCore and all threads are created and started.

This work fine and i assume that this is thread safe.

When does the Parts are dispose? When the GC dispose the UnitCore instance or when the container is dispose?

Did i need to manually dispose the container or catalog?

Now i want to add the recomposition capability to the ImportMany of IEnumerable<Lazy<IProcess, IProcessData>>. That will allow me to add *.dll file into the Extension directory, use by the DirectoryCatalog.

I just need a little help for safe threading the recomposition.

Can i call the DirectoryCatalog.Refresh() (with the main thread) at any time without risk? If there is a risk, how can i handle this?

Does the recomposition change something on the way the Parts are dispose? 

Thank you and sorry for my bad English...