The application called an interface that was marshalled for a different thread - Windows Store App
This solved my issue:
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
}
);
Correct way to get the CoreDispatcher in a Windows Store app
uwp - 'The application called an interface that was marshalled for a different thread
Solution:
so battling to make the chat work this was the problem:
await CoreApplication.MainView
since the chat is in a different view than the main page it couldn't find the binding or the xaml element.
what my friend realize that in order to make changes in other view you need to get its object so, we added this property to the view model:
public CoreApplicationView CurrentView { get; set; }
when launching the chat room view we set the property:
public UserViewModel userViewModel;
public ChatRoom(UserViewModel userViewModel)
{
this.InitializeComponent();
this.userViewModel = userViewModel;
userViewModel.ChatTbl = chatTbl;
userViewModel.CurrentView = CoreApplication.GetCurrentView();//this line
}
now when we need to make changes in the view we use:
_hubProxy.On<string, string>("SendMessage", async (sender, message) =>
{
await CurrentView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
ChatTbl.Text += $"{sender}: {message} \n";
});
});
so instead await CoreApplication.MainView
use await CurrentView.CoreWindow
hope this would help someone i know it would have helped me
the application called an interface that was marshalled for a different thread. windows 8.1 store app
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 = "";
}
);
}
The application called an interface that was marshalled for a different thread - Xamarin Forms
The problem was simple. I'd added code that wasn't necessary, and that code was causing the bug. Here is the fixed method:
protected async override void OnAppearing()
{
base.OnAppearing();
await Initialise();
}
The Band application called an interface that was marshalled for a different thread
The event is raised on a background thread. Use CoreDispatcher.RunAsync
to marshal it back to the UI thread:
private async void HeartRate_ReadingChanged(object sender, Microsoft.Band.Sensors.BandSensorReadingEventArgs<Microsoft.Band.Sensors.IBandHeartRateReading> e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
txtStatus.Text = string.Format("Current Heart Rate is: {0}", e.SensorReading.HeartRate.ToString())
}).AsTask();
}
Xamarin Forms - The application called an interface that was marshalled for a different thread
A System.Threading.Thread.Timer
's Elapsed event is run on a Thread Pool thread, which doesn't own ACD0 (which I assume is the interface for that device). Instead, you should use the Xamarin Forms Timer:
Threading
Any code run in a timer will run on the main UI Thread. Make sure you don’t block the UI thread or do any intensive calculations. Ensure you move the code to a background thread if appropriate.
public void InitSecondTimer(int interval)
{
Device.StartTimer(TimeSpan.FromMiliseconds(interval), () =>
{
double voltage = 0;
InputI2C(ADC0, ref voltage); //Read data from I2C devices
ai0.Text = voltage.ToString(); //ai0 is a Label
return true; // True = Repeat again, False = Stop the timer
});
Debug.WriteLine("Secondtimer inited");
}
UWP IOT-Core App System.Exception: The application called an interface that was marshalled for a different thread
Like Tobonaut said, you can use the Dispatcher.RunAsync
to call the Navigation
, it worked.
But your problem may not be this.
I copied your code and reproduced your problem and found that you have problems with the calls to read and write files:
// Your code
StorageFile csvFile = await storageFolder.CreateFileAsync(csvName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
StorageFile file = await KnownFolders.MusicLibrary.GetFileAsync(csvName).AsTask().ConfigureAwait(false);
The Navigation will be work if you delete the .AsTask().ConfigureAwait(false)
.
Best regards.
Related Topics
How to Get Status Code from Webclient
C#: How to Get First Char of a String
How to Generate Truly (Not Pseudo) Random Numbers with C#
Visual Studio 2010 Conditional References
Enumerating Collections That Are Not Inherently Ienumerable
Inheriting Comments from an Interface in an Implementing Class
How to Get the Current Date Without the Time
Are There Good Reasons Not to Use an Orm
Using Profiles in Automapper to Map the Same Types with Different Logic
How to Check If a Property Exists on a Dynamic Anonymous Type in C#
Lowering Priority of Task.Factory.Startnew Thread
How to Solve Disturbance in My Bot in C#