Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

dispatcher.invoke called from Word addIn - hangs word and itself

$
0
0

Hi,

I originally asked my question here and also got an answer, but discovered more issues afterwards. The problem seems to be complicated, so I will try to be as detailed as possible.

I am using a WordAddIn that added a custom button to the Ribbon to call a WPF object that displays a graph representing relation ships in between documents. The current code to call the WPF looks like this:

System.Windows.Application app = null; 
public void OnTextButton(Office.IRibbonControl control)
{
     if (app == null)
     {
         app = new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
         app.Run();
      }
      app.Dispatcher.Invoke((Action)(() => { new MainWindow(graph); }));
}

Which leads to the following behavior: The first time the button is clicked, nothing happens but the cursor changes to busy and it is no longer possible to edit the word document. However, it is still possible to use the ribbon, so the button can be pressed a second time, which functions fine then. The WPF is created and shown. I suspect that somehow Word and the dispatcher get into a loop.

The Invoke action does not get processed in the first try, I placed a

System.Diagnostics.Debug.WriteLine("Mainwindow fired");

Inside the MainWindow class to see if it is created. The line is shown for every button click after the first.

After doing some research on the subject, I tried the following solution:

app.Dispatcher.BeginInvoke((DispatcherPriority.Send, Action)(() => { new MainWindow(graph); }));

Which leads to the same result. I tried:

if (app == null)
{
     app = new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
     app.Run(new MainWindow(graph));
}
else
{
     app.Dispatcher.Invoke((Action)(() => { new MainWindow(graph); }));
}

Which will lead to the button to work the first time, but not after. Word still freezes. Just running the run command and not the dispatcher will lead to the button working the first  time (word still freezes). The second time will call an exception:"Cannot create more than one System.Windows.Application instance in the same AppDomain."

Any clue on what is going on here would be greatly appreciated, or at least hints on how to tell Visual Studio to show me whats going wrong and where.

Thanks alot,

Michael


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>