How can a thread update a textbox in the page form that is part of the main thread?

Viewed 86

I'm creating a UWP APP to receive the data from ADC board, I was used WindForm to develop it, it can work, but because UWP doesn't support using Control.Invoke look through the papers, but I still have no idea how can I start, how can I convert the windform code to UWP code?

The windform code as below

 public async Task StartProcess()

        {
            IsCancelled = false;
            Task t = new Task(() =>
            {
                while (IsCancelled == false)
                {
                    I = I + 1;
                    SetTextBox(I);

                }
            });
            t.Start();
        }

     public void SetTextBox(int _Val)
    {
        if (InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate () { SetTextBox(_Val); });
            return;
        }
        textBox1.Text = Convert.ToString(_Val);
    }
1 Answers

How can a thread update a textbox in the page form that is part of the main thread?

In UWP platform we need to update UI in main thread, You could use Dispatcher to switch to main thread then update ui.

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
  // Update  Textblock
});
Related