Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XamlReader.Load in a Background Thread. Is it possible?

A WPF app has an operation of loading a user control from a separate file using XamlReader.Load() method:

StreamReader mysr = new StreamReader(pathToFile);
DependencyObject rootObject = XamlReader.Load(mysr.BaseStream) as DependencyObject;
ContentControl displayPage = FindName("displayContentControl") as ContentControl;
displayPage.Content = rootObject;

The process takes some time due to the size of the file, so UI becomes frozen for several seconds.

For keeping the app responsive I try to use a Background thread for performing the part of operation that is not directly involed in UI updating.

When trying to use BackgroundWorker I got an error: The calling thread must be STA, because many UI components require this

So, I went another way:

 private Thread _backgroundThread;
 _backgroundThread = new Thread(DoReadFile);
 _backgroundThread.SetApartmentState(ApartmentState.STA);
 _backgroundThread.Start();
 void DoReadFile()
 {
   StreamReader mysr3 = new StreamReader(path2);
   Dispatcher.BeginInvoke(
           DispatcherPriority.Normal,
           (Action<StreamReader>)FinishedReading,
           mysr3);
 }

 void FinishedReading(StreamReader stream)
    {            
        DependencyObject rootObject = XamlReader.Load(stream.BaseStream) as DependencyObject;
        ContentControl displayPage = FindName("displayContentControl") as ContentControl;
        displayPage.Content = rootObject;
    }

This solves nothing because all time consuming operations remain in UI thread.

When I try like this, making all parsing in the background:

private Thread _backgroundThread;
_backgroundThread = new Thread(DoReadFile);
_backgroundThread.SetApartmentState(ApartmentState.STA);
_backgroundThread.Start();
 void DoReadFile()
 {
  StreamReader mysr3 = new StreamReader(path2);      
  DependencyObject rootObject3 = XamlReader.Load(mysr3.BaseStream) as DependencyObject;
        Dispatcher.BeginInvoke(
           DispatcherPriority.Normal,
           (Action<DependencyObject>)FinishedReading,
           rootObject3);
  }

  void FinishedReading(DependencyObject rootObject)
  {            
    ContentControl displayPage = FindName("displayContentControl") as ContentControl;
    displayPage.Content = rootObject;
  } 

I got an exception: The calling thread cannot access this object because a different thread owns it. (in the loaded UserControl there are other controls present which maybe give the error)

Is there any way to perform this operation in such a way the UI to be responsive?

like image 918
rem Avatar asked Mar 22 '11 17:03

rem


3 Answers

Getting the XAML to load an a background thread is essentially a non-starter. WPF components have thread affinity and are generally speaking only usable from the threads they are created one. So loading on a background thread will make the UI responsive but create components which then cannot be plugged into the UI thread.

The best option you have here is to break up the XAML file into smaller pieces and incrementally load them in the UI thread making sure to allow for a message pump in between every load operation. Possibly using BeginInvoke on the Dispatcher object to schedule the loads.

like image 189
JaredPar Avatar answered Oct 12 '22 22:10

JaredPar


As you found out, you can't use XamlReader.Load unless the thread is STA and even if it is, you will have to have it start a message pump and funnel all access to the controls it created through that. This is a fundamental way of how WPF works and you can't go against it.

So your only real options are:

  1. Break down the XAML into smaller pieces.
  2. Start a new STA thread for each Load call. After Load returns, the thread will need to start a message loop and manage the controls it created. Your application will have to take into account the fact that different controls are now owned by different threads.
like image 27
Jon Avatar answered Oct 12 '22 23:10

Jon


I don't have exact solution but you can get some direction from following links.

http://www.codehosting.net/blog/BlogEngine/post/Opening-WPF-Windows-on-a-new-thread.aspx

http://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/

like image 29
Akash Kava Avatar answered Oct 13 '22 00:10

Akash Kava