0

I want to call Update(int i) from inside of the Thread. I used to use BeginInvoke(), but it isn't available in UWP.

public class Viewmodel
{
 Viewmodel()
 {
   //start task
   var mainTask = Task.Factory.StartNew(() =>
   {
     MyThread();
   });
 }
 private void MyThread()
 {
    int i;
    while(condition)
    {
      //do somethings
      Update(i) 
    }
 }
 private Update(int i)
 {
   //do somethings
 }
}

2 Answers2

2

The below should work.

Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    // update your UI here
    Update(i) 
});
thunderbird
  • 2,715
  • 5
  • 27
  • 51
-1

You can use the below code to run your code :

await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.High/* the rank */,
() =>
{
    // run the code
});

And I have another way to do it

  await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal /*the rank*/, () => { // run the code  });

See https://stackoverflow.com/a/38175976/6116637

lindexi
  • 4,182
  • 3
  • 19
  • 65