WPF LifeCyle Add-in

I created an add-in that is called through Reflection, a WPF class library. Since this is a class library, I had to manually initialize new System.Windows.Application().

Then the class constructor (the one called through reflection) creates a window, and Show () (with Dispatcher.Run () to close the window immediately) or ShowDialog ().

Since my application is in an add-in, the application is still live. Therefore, I can install it only once.

At the first start (when the application starts) Application.Current.Dispatcher is launched.

But in the second run, I let go that Application.Current.Dispatcher was stopped. I never call InvokeShutdown (), so I do not understand when the dispatcher is stopped.

When I run this a second time, the application is already instantiated (this is normal), but the dispatcher stopped.

Any idea? Thank!

Edit: In my add-in, I tried two ways:

The first way:

        foreach (Type type in ass2_l.GetTypes())
        {
            if (type.Name == "Loader")
            {
                object obj_l = Activator.CreateInstance(type);
                BindingFlags bf_l = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
                object[] argList_l = new object[1];
                argList_l[0] = "ok";
                type.InvokeMember("Load", bf_l, null, obj_l, argList_l);
            }
        }

When I call the dll directly from the add-in, Application.Current.Dispatcher is in the background with the name "VSTA_Main". When I run the second time, the dispatcher is still in the background.

The second way:

        t_m = new Thread(loadDll);
        t_m.SetApartmentState(ApartmentState.STA);
        t_m.Start();

loadDll actually contains the same "first way" code code. When I run this part for the first time, the dispatcher is working, and everything is perfectly fine. When starting a second time, the dispatcher stops.

2: . loadDll , , t_m , , ManagedThreadId t_m ManagerThreadId:/

3: . , , . WPF DLL ( ), ( ), "" , "", (, )

+3
2

, , , AutoResetEvent/ManualResetEvent, / ...

, ( , dll WPF UI, ), ( ), .

, :)

+1

ThisAddin.Startup :

private void ThisAddInStartup(object sender, EventArgs e)
{
    if (System.Windows.Application.Current == null)
        new System.Windows.Application();
    System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown
}

, .

+7

All Articles