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

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -