Getting Error as soon as i run window application containing Element Host control on Splash screen
-
Thursday, August 02, 2012 7:05 AM
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
Dim Thread As New Threading.Thread(AddressOf AddCtrol)
Threading.SetApartmentState(Threading.ApartmentState.STA)
Thread.Start()
Thread.Join()
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.
All Replies
-
Thursday, August 02, 2012 7:51 AM
Hi Prajyot
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.
Regards
Yoni
- Marked As Answer by Annabella LuoModerator Monday, August 20, 2012 3:09 AM

