When I execute the updateScreen() function, an exception is thrown when the new value is set in the TextLabel string. This exception is demonstrated in the figure right after the code.
This error occurs when I invoke the screen update through the INotifyPropertyChanged interface or through the method of the ObservableObject class, after creating a new Thread.
My code:
Код: Выделить всё
public class PageInicialViewModel : ObservableObject
{
private int cont = 0;
private string _textLabel = 0.ToString();
public string TextLabel
{
get => _textLabel;
set => SetProperty(ref _textLabel, value);
}
public void updateScreen()
{
Task.Factory.StartNew(updateTextLabel);
}
public void updateTextLabel()
{
while (true)
{
cont++;
TextLabel = cont.ToString();
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
Источник: https://stackoverflow.com/questions/710 ... the-view-d