Dedicated thread window w animations best practices for bindings

LT 101 Reputation points
2021-07-03T12:47:04.007+00:00

Hi!
I have difficulties with smooth animations. Therefore I decided to try out having the main window on it's own thread.
I have not found any example/practices for how to handle bindings.
Basically everything the window needs is in the datacontext, but of course then what i am getting is:
The calling thread cannot access this object because a different thread owns it.

Is there a source where I could read about a proper way to implement this?

Does anyone have experience with this approach in regard to animations?

Thanks!

I use this code for the dedicated thread:

   Thread thread = new Thread(() =>
        {

            TopWindow = new Window();
            TopWindow.DataContext = TopModel;


            TopWindow.Show();

            TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();

            System.Windows.Threading.Dispatcher.Run();

        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 81,721 Reputation points
    2021-07-03T13:25:00.513+00:00

    For smooth animations, you can see the MS samples : Animation

    111543-animation-gallery.jpg

    1 person found this answer helpful.

  2. LT 101 Reputation points
    2021-07-03T13:58:20.197+00:00

    Sorry in my case it's wpf storyboards and control animations..

    0 comments No comments

  3. Emon Haque 3,176 Reputation points
    2021-07-03T17:43:16.83+00:00

    Create your TopModel on that thread like this:

    Thread thread = new Thread(() =>
         {
             TopWindow = new Window();
             TopWindow.DataContext = new TopModel();
             TopWindow.Show();
             TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();
             System.Windows.Threading.Dispatcher.Run();
         });
         thread.SetApartmentState(ApartmentState.STA);
         thread.IsBackground = true;
         thread.Start();
    

    you might need the TopWindow object to Close the window later so you can have a reference of that like this:

    TopWindow TopWindow = null;
    Thread thread = new Thread(() =>
         {
             TopWindow = new Window();
             TopWindow.DataContext = new TopModel();
             TopWindow.Show();
             TopWindow.Closed += (sender1, e1) => TopWindow.Dispatcher.InvokeShutdown();
             System.Windows.Threading.Dispatcher.Run();
         });
         thread.SetApartmentState(ApartmentState.STA);
         thread.IsBackground = true;
         thread.Start();
    

    and later you can close the window like this:

    TopWindow.Dispatcher.Invoke(TopWindow.Close);
    

    You also don't need a ViewModel, TopModel, for such window that is overlaid on MainWindow for some splash animation. You create the animation in the constructor or somewhere in the window and hook that animation into the Loaded/Activated event of that window.