locked
Loading dialog in a new thread RRS feed

  • Question

  • I wanted to show a loading dialog untill a new window opens.This new window takes up some time to open,so I decided to use the Background Worker class and its dowork method.But it threw an exception saying "the thread is not a STA thread".How do I show a loading box in a window untill the new window loads.
    Thursday, January 26, 2012 10:38 AM

Answers

All replies

  • Have a usercontrol or whatever containing a throbber which is incorporated in all main windows and hidden most of the time.

    Before you instantiate a new window, first show the throbber then hand over a reference to your new window.

    Once the new window has focus, hide the throbber via your passed reference.

    • Marked as answer by Jie Bao Monday, February 6, 2012 4:58 AM
    Thursday, January 26, 2012 11:39 AM
  • PS

    Instead of passing a reference you could instead use one of the messaging frameworks.

    MVVM light or Prism.  I would go with MVVM Light.

    Essentially, the window with the throbber would subscribe for a "stop throbber" message.  The new window would send that message when it's got focus.  That avoids the dependency introduced by passing a reference from one window to another.

    Thursday, January 26, 2012 11:53 AM
  • Please refer to this blog to know how to implement the multi-UI threads in WPF: http://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/

    But your design may make a mistake. You should not make the new window UI take up much time. Please move your complex logical into the background worker, other thread, and show a simple loading dialog in the UI thread to update the progress. Then and progress update could set by the UI thread Dispatcher.Invoke or BeginInvoke. Such as my sample below: https://skydrive.live.com/embed?cid=51B2FDD068799D15&resid=51B2FDD068799D15%21627&authkey=AL_B8SANw-w09lc

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Jie Bao Monday, February 6, 2012 4:58 AM
    Friday, January 27, 2012 8:23 AM