1

I am developing a windows store 8.1 app using C# and xaml. When i tried to update the UI from code behind file i got an exception which is

" the application called an interface that was marshalled for a different thread"

i have added the below code to update the UI

 await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { statustextblk.Text = "Offline sync in progress..."; } ); 

This is working fine. But i want to update the same textblack after offline sync is completed. So for this i wrote the below code, the code looks like this

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { statustextblk.Text = "Offline sync in progress..."; } ); await SharedContext.Context.OfflineStore.ScheduleFlushQueuedRequestsAsync(); Debug.WriteLine("Refresh started"); if (SharedContext.Context.OfflineStore != null && SharedContext.Context.OfflineStore.Open) await SharedContext.Context.OfflineStore.ScheduleRefreshAsync(); RefreshSuccess = true; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { statustextblk.Text = "Offline sync completed..."; Task.Delay(1000); statustextblk.Text = ""; } ); 

But it is not displaying the "Offline sync completed..." message in the UI.

How can i show it in the UI once the method is executed?

Can someone please help me out?

Thanks in advance

2
  • You didn't await the Task.Delay(1000) so the next line (which clears the message) runs immediately. Commented Sep 27, 2016 at 6:23
  • Hey Raymond.. Thanks for your reply. it is not even displaying "Offline sync completed..." this message in the UI. I commented Task.Delay(1000); this line as well Commented Sep 27, 2016 at 8:27

1 Answer 1

3

If you comment Task.Delay(1000), "Offline sync completed..." will show within a very short time but will disappear immediately because you set the statustextblk.Text = "".

Therefore you can refer to @Raymond said in the comment. Add modifier ‘async’ before () => and modifier ‘await’ before Task.Delay(1000); You can refer to the demo I made as following:

private async void Button_Click(object sender, RoutedEventArgs e) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { statustextblk.Text = "Offline sync in progress..."; } ); Debug.WriteLine("Refresh started"); await Task.Delay(1000); //RefreshSuccess = true; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { statustextblk.Text = "Offline sync completed..."; await Task.Delay(1000); statustextblk.Text = ""; } ); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, if it solves your problem, please mark as an answer so that other visitors can refer to it, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.