Getting Error as soon as i run window application containing Element Host control on Splash screen

Con risposta Getting Error as soon as i run window application containing Element Host control on Splash screen

  • 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

     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.

Tutte le risposte

  • giovedì 2 agosto 2012 07:51
     
     Con risposta

    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