Click here to Skip to main content
14,453,401 members
Rate this:
Please Sign up or sign in to vote.
See more:
i was trying get the details using task asynchronously and
update ui
.but i always getting the exception
System.Windows.Markup.XamlParseException: 'Must create DependencySource on same Thread as the DependencyObject.

What I have tried:

public void performwindowchanges()
{
 Task<bool> t = Task.Run(async () =>
                        {
                            string authUrl ="url";
                            string data = await NetworkUtility.GetInstance().GetHttpRequestAsync(authUrl, 1);
                            return true;
                        });

                      t.ContinueWith(async (t1) =>
                        {
                            if (t1.Result)
                            {
                              string  status = await Applicationmeth.GetUserDetails();
                                if (status == "Success")
                                     this.Dispatcher.Invoke(() =>
                                  {
                                    InitializeMainwindow();//facing issue in this method
                                  });
                            }
                        });
}

Private void InitializeMainwindow()
{
                          screenWindow = new ScreenWindowControl(); 
                        Application.Current.MainWindow = screenWindow;
                    screenWindow.Show();//HERE IAM GETTING EXCEPTION AS Must create DependencySource on same Thread as the DependencyObject.
                    this.Close();
}
Posted
Comments
Richard MacCutchan 9-Feb-19 5:36am
   
You are trying to update the UI from a background thread, which is not allowed. Google for "UI Crossthread XAML" and you will find lots of suggestions.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100