Search code examples
c#wpfmultithreadingappdomainmaf

UI thread for each MAF AddIn


I work with the MAF in WPF and I have problems with the UI threads. I would like that each AddIn that I activate become a unique UI thread.

Why do I want that you probably wondering? I try to explain :-)

I have a host application which is a container for addins. Different people developing addins for the host. If a developer start a long procedure in their addin and they forget to Invoke or start the procedure in a thread, the host will freeze because the host and the addins share the same UI thread.

Every addin run in a seperatly appdomain. About every hint I would be happy.


Solution

  • you could try the following approach:

    Thread thread = new Thread(() =>
    {
     Window1 w = new Window1();
     w.Show();
    
     //make sure to stop the Dispatcher when window is closed
     w.Closed += (s, e) => w.Dispatcher.InvokeShutdown();
     //start the message pump
     System.Windows.Threading.Dispatcher.Run();
    });
    
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();