giovedì 2 agosto 2012 07:05
I have a splash screen in my window application where i want to run a WPF animation. As soon as i run the application i get and error saying, "System.InvalidOperationException was unhandled
Message=The calling thread must be STA, because many UI components require this.".
I changed the threadmodel to STA like this
And in the delegate funtion AddCtrol I have tried to create object of Element Host at runtime and add in splash screen, but as there two controls run on different threads i cannot add the Element host in the splash screen controls collection. I get this error, "System.ArgumentException was unhandled Message = Controls created on one thread cannot be parented to a control on a different thread.". Is there a way out or am i missing any thing.
Tutte le risposte
giovedì 2 agosto 2012 07:51
Any UI manipulation must be called from the main thread. Only that thread can change the UI, doing it in any other will only would throw this exception. In any case, creating a thread by yourself is a bad practice, call it directly from the main thread, or consider using ThreadPool or a BackgroundWorker instead.
If you call it from another thread, using the Dispatcher when actually creating the controls should solve your problems.
- Contrassegnato come risposta Annabella LuoModerator lunedì 20 agosto 2012 03:09