(UWP C#) The application accessed an interface related to another thread

I found an example of implementing a console TCP chat in C# and decided to try to transfer its client to UWP. In the example, there were two applications, a client and a server. As a server, I use the server console application from the example and connect to it using the UWP client, instead of the console application from the example. But that's not the point.
The bottom line is as follows. There is a method ReceiveMessage ():

public void ReceiveMessage()
    {
        while (true)
        {
            try
            {
                byte[] data = new byte[64];
                StringBuilder builder = new StringBuilder();
                int bytes = 0;
                do
                {
                    bytes = SettingsPage.stream.Read(data, 0, data.Length);
                    builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                }
                while (SettingsPage.stream.DataAvailable);

                ChatPage.Message = builder.ToString();                  
                //MessageWriterAsync(ChatPage.Message);
                
            }
            catch
            {
                ChatPage.Message = "Подключение прервано";
                ChatPage.DisconnectRequest();
            }
        }

    }

I use this method to get a message and it works in a thread, which in turn starts when you connect to the server. When I receive a message, I execute the MessageWriterAsync(string MessageToWrite) code, to which I send the Message string from the ChatPage:

public async void MessageWriterAsync(string MessageToWrite)
    {
        ChatPage Ch = new ChatPage();


        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            while (true)
            {
                if (MessageToWrite != "")
                {
                    Ch.boxChat.Text = MessageToWrite;
                }
            }
        });
    }

Which will write the received message (the Message variable) to a TextBox named boxChat. But to my regret, it doesn't work( If I remove the commenting in the ReceiveMessage() method before calling MessageWriterAsync, I get exception

[![Exception][1]][1]

Please help me(((
Explain to the teapot what it is doing wrong?


UPD 9.01.2020 19:10

Attached the ChatHistory string variable to boxChat.Text, for which I created a separate Strings class in the Resources folder:

public class Strings : INotifyPropertyChanged
    {
        private string _ChatHistory;
        public string ChatHistory 
        {

            get { return _ChatHistory; } 
            
            set
            {
                if (_ChatHistory != value)
                {
                    _ChatHistory = value;
                    OnPropertyChanged("ChatHistory");
                }
            } 

        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

    }

Then in the xaml page with boxChat added

<Page ...
    ...
    xmlns:local="using:MyNamespace.Resources"
    ...
    <Page.DataContext>
        <local:Strings ChatHistory="История чата" />
    </Page.DataContext>
    <Grid>
    <TextBox x:Name="boxChat" Text="{Binding ChatHistory}"/>
    </Grid>
</Page>

And in the code of this page added:

   public ChatPage()
        {
            this.InitializeComponent();
            this.DataContext = new Strings { ChatHistory = "История чата" };
            connected = false;
        }
   ...
   public void AddString(string String)
        {
            Strings strings = this.DataContext as Strings;
            strings.ChatHistory += "\r\n" + String;
        }
   ...

Then I changed ReceiveMessage () a bit

   void ReceiveMessage()
        {
            while (true)
            {
                try
                {
                    byte[] data = new byte[64];
                    StringBuilder builder = new StringBuilder();
                    int bytes = 0;
                    do
                    {
                        bytes = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }
                    while (stream.DataAvailable);

                    string message = builder.ToString();
                    Strings StringsLink = new Strings();
                    StringsLink.ChatHistory += "\r\n" + message;

                }
                catch
                {

                }
            }
        }

Now when calling AddString (for example, when a button is clicked) my boxChat updates and adds a conditional string string. But when receiving a message using Receivemessage (), my Chathistory string gets the value of message but boxChat for some reason does not update this value. I'll keep digging.

Author: Oxxxygen, 2021-01-07