Threading issue when trying to recompose controls

Jan 22, 2010 at 2:52 PM
Edited Jan 22, 2010 at 2:58 PM

Hi all,

I'm trying to import and load dynamically some GUI controls with Recomposition set to true. When doing so, I got the following unhandled RecompositionException:

1) The calling thread must be STA, because many UI components require this.

It was unable to create instances of the control types.

How can I manage to solve this?

Thanks in advance,




Jan 26, 2010 at 2:22 PM
Edited Jan 26, 2010 at 2:23 PM

I think I know where my problem came from. I'm trying to access the controls in the event handler of FileSystemWatcher (example below).

It works fine now with my controls being instantiated elsewhere :)

[ImportMany(typeof(ICustomGui), AllowRecomposition = true)]
private Lazy<ICustomGui>[] _guis;

fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);

void OnChanged(object sender, FileSystemEventArgs e)

  foreach (var item in _guis)
       var accessGui = _guis.Value;//exception occurs




Jan 26, 2010 at 4:30 PM

By default FileSystemWatcher makes no guarantee which thread the callback is going to be called on so it is generally dangerous to access objects in the callback without dispatching to the appropriate thread. Also in the case of UI controls they generally have to be constructed and access on the main thread or they may not work correctly.