Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900