c# - Creating objects inside async thread causes STA exception -


i have class, should show messages user, when status of operation changed, this:

public static class autoupdater     {         public static async void autoupdatecheck()         {             updaterstatus.currentupdatestatus = await updaterlogic.checkforupdateasync();         }          public static void onstatuschanged()         {                 switch (updaterstatus.currentstatus)                 {                     case updatestatus.updatefound:                         {                             message toadd = new message("some params"); //exception here                             messagesmanager.addnewmessage(toadd);                             break;                         }                     //some other cases                 }         } 

when app starts, subscribe autoupdater event this:

updaterstatus.eventstatuschanged += (sender, args) => { autoupdater.onstatuschanged(); }; 

the exception is: "the calling thread must sta, because many ui components require this".

however, can't create sta thread myself, , add newly created message parent control, because way exception, saying "that object belongs thread".

is there workaround?

you should create ui controls ui thread. can try use dispatcher here

application.current.dispatcher.invoke(/* action here*/ () => {/* creating ui controls */}); 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -