c# - WPF new window creation on new thread error -


void itemcommand_click(office.commandbarbutton ctrl, ref bool canceldefault) {     var thread = new thread(() =>     {     if (logincheck())     {         itemwindow itw = new itemwindow();          //dispatcher.currentdispatcher.invoke((system.action)(() =>         //{               itw.show();               itw.closed += (sender2, e2) => { itw.dispatcher.invokeshutdown(); };         //}));          dispatcher.run();      }      });      thread.setapartmentstate(apartmentstate.sta);     thread.start();  } 

i keep getting error message of "the calling thread cannot access object because different thread owns it." on line "itw.show();" when function called twice. works fine first call, , after window closed , trying open again, fails. commented out "invoke" method, doesn't work dispatcher either. please me find solution. thank you.

----------------- edit

the reason why i'm creating new thread because excel addin. can't create windows main thread excel collides windows if create them main thread.
thing don't understand why new instance(itemwindow) new thread collide old thread.

i created simple test method in new application called when click (only) button on main form. method looks this:

private void button_click(object sender, routedeventargs e) {     thread thread = new thread(() =>     {         window1 window = new window1();         window.closed += (s, a) => window.dispatcher.invokeshutdown();         window.show();         system.windows.threading.dispatcher.run();     });      thread.setapartmentstate(apartmentstate.sta);     thread.start(); } 

window1 window class made has nothing single textblock on it. can click button many times want, , continues open new windows no issues (regardless whether or not close previous 1 first).

i suspect issue occurring in code not showing somewhere. need careful nothing on new thread attempts access ui related main thread. windows running on separate threads cannot communicate each other unless go through dispatcher of other thread. exception seeing thrown when method or property of dispatcherobject accessed thread other 1 created object.

taking step back, why important new window on own thread? unless new window going monopolize thread, run fine on main thread. if running long blocking operation, perhaps operation alone should moved thread rather entire window. don't know doing exactly, think about.


edit: realizing may not running in typical wpf application (looks might in office plugin), updated test launch windows standalone on own threads. however, still able launch 2 windows in row no issues.

here new test. method , test class window1 entirety of application.

[stathread] public static int main(string[] args) {     threadstart threadfunc = () =>     {         window1 window = new window1();         window.closed += (s, a) => window.dispatcher.invokeshutdown();         window.show();         system.windows.threading.dispatcher.run();     };      thread thread = new thread(threadfunc);     thread.setapartmentstate(apartmentstate.sta);     thread.start();     thread.join();      thread = new thread(threadfunc);     thread.setapartmentstate(apartmentstate.sta);     thread.start();     thread.join();      return 0; } 

so, there appears nothing inherently wrong trying do, nor see obvious issue in code. suspect there invalid cross-thread communication happening somewhere in custom window while being shown. (either that, or running issue specific office plugins.)


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -