Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends, so after more than a week of trying to solve it on my own I officially give up and turn to your help. Basically, it should not be so complicated so I have no idea why it does not work. I have a WPF app which contains a Main Window called surprise surpise...: Main_Window.
That window contain a user control called 'pageTransitionControl' that change its content according to what the client want to see. the 'pageTransitionControl' is there to support multiple animations and so on... Anyway, among all of the user controls,  i have a preety havy uc called ucBanks. before it shows, the ucBanks load a lot of data, manipulating it and display it on a very beautiful and smart charts. the problem is it takes some time to load it, approximately 6-7 seconds so i need the UI to show 'Loading' animation during that time (another user control called 'ucSpinner').
I'm Trying to load the ucBanks on a different thread to avoid freezing the application and it works great: the ucSpinner is showed immidiatlly and the ucBanks is loading on the background but when i change the content of the 'pageTransitionControl' i get this error:
"The calling thread cannot access this object because a different thread owns it".
I think i tried basically everything but i must missing somthing or doing somthing wrong.
This is where it all start, the btn_click event that load ucBanks:

ShowSpinner();
Thread.Sleep(100);
Thread newThread = new Thread(new ThreadStart(LoadUc));
newThread.SetApartmentState(ApartmentState.STA);
newThread.IsBackground = true;
newThread.Start();

This is the ShowSpinner method:

<pre lang="c#">private void ShowSpinner()
{
   ucSpinner.Opacity = 1;
}


and this is the LoadUc method:

C#
private void LoadUc()
{
     ucOsh ucOshx = new ucOsh();
     Utils.LoadUc(ucOshx, null, PageTransitions.PageTransitionType.GrowAndFade, true, this, null, true);
}


With the LoadUc i called static class called 'Utils' holding the 'LoadUc' method:

C#
public static void LoadUc(System.Windows.Controls.UserControl ucParent, System.Windows.Controls.UserControl ucChild, PageTransitions.PageTransitionType tranType, bool removeChildrens = true, System.Windows.Window w = null, List<Plist.Plist> lst = null, bool hideMenu = false)
        {
            MainWindow win = null;
            if (w != null) { win = (MainWindow)w; }
            else { win = (MainWindow)System.Windows.Window.GetWindow(ucChild); }
            win.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.ContextIdle, (System.Action)delegate
            {
                win.pageTransitionControl.TransitionType = tranType;
                win.pageTransitionControl.PARAMS = lst;
                win.pageTransitionControl.Tag = ucParent.ToString();
                win.pageTransitionControl.pages.Push(ucParent);
                win.pageTransitionControl.Content = ucParent;  ----------->>>>This is where i get the error!!!
            });
        }


I understand that the main window is locked inside another thread but i cant see any other option to load it without freezing the entire app.
Does anyone have a suloution to my problem? SA :-) ?

What I have tried:

i tried working with background-worker, i chaned all of the settings of the dispatcher, loaded the user control inside and outside the threads...
Posted
Updated 27-May-17 20:28pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900