c# - Open WPF Application from Excel VSTO project -


i have vsto excel tool written in c# , use wpf form display data. wpf form in different project, in same solution excel vsto project. if open wpf form in same thread excel, somehow damage excel , starts doing strange things. if run wpf form in different thread, works perfectly. code below should ok:

if (app != null) { // when click button again , wpf form opened already, bring on top.     bringdatabasetofront(); } else {     t = new thread(() =>     {         app = new app(_synchronizationcontext, currentcaller);          app.resourceassembly = app.gettype().assembly;          app.initializecomponent();          app.shutdownmode = system.windows.shutdownmode.onmainwindowclose;          /* makes thread support message pumping         * dispecher context of wpf db form  */         dispatcher.run();     });      // wpf must on single-threaded apartment     t.setapartmentstate(apartmentstate.sta);     t.start(); } 

the problem starts when shut-down tool (excel). freezes , stays frozen forever. have tried many different way shut-down wpf form, 1 works , closes is:

public void shutdownattempt() {     environment.exit(0); }    

that works, have problem. if users have several other excel workbooks opened, code closes of them without warning, have data not saved. dangerous.

it looks it’s not easy shut down wpf application if running on thread main application. have experience using wpf form excel?

try call shutdown() method of app using dispatcher:

app.dispatcher.invoke(()=> { app.shutdown(); }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -