c# - The calling thread must be STA, because many UI components require this in WPF -
my scenario:
void installer1_afterinstall(object sender, installeventargs e) { try { mainwindow objmain = new mainwindow(); objmain.show(); } catch (exception ex) { log.write(ex); } }
i got error "the calling thread must sta, because many ui components require this"
what do?
normally, entry point method threads wpf have [stathreadattribute]
set threadmethod
, or have apartment state set sta when creating thread using thread.setapartmentstate()
. however, can set before thread started.
if cannot apply attribute entry point of application of thread performing task from, try following:
void installer1_afterinstall(object sender, installeventargs e) { var thread = new thread(new threadstart(displayformthread)); thread.setapartmentstate(apartmentstate.sta); thread.start(); thread.join(); } private void displayformthread() { try { mainwindow objmain = new mainwindow(); objmain.show(); objmain.closed += (s, e) => system.windows.threading.dispatcher.exitallframes(); system.windows.threading.dispatcher.run(); } catch (exception ex) { log.write(ex); } }
Comments
Post a Comment