UWP update UI from Task

2019-01-06 23:56发布

问题:

I have application, which is checking network ranges (for running http service) in local network.

So it means, that I am checking f.e. from 10.0.0.1 to 10.0.0.255. And here is the problem, when running on PC, the speed is enough, but when running on Lumia 950 the speed is not enough. So I would like to update the UI during the scan.

So here are the questions:

  1. At this moment I have several tasks f.e. 10 - every task is scanning his range f.e. task 1 - 10.0.0.1 to 10.0.0.25 etc.. - should I use 10 tasks or is there some way, how the .net will solve it itself? What will be the performance, f.e. if I will use 50 tasks?

  2. Second question is, during the scan I will find the PC, where the web service is working, but... How should I update the UI when the PC is found? At this moment I am only able to do it, when all tasks are finished..

The methods I am calling are async Tasks

回答1:

  1. You can use as many tasks as you want, the system will automatically perform queuing and execution for you. That's one of the advantages of using the built in mechanisms

  2. To execute code on the UI thread you can use the dispatcher like this:

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                //UI code here
    });
    


回答2:

To execute code on the UI thread and you only want to update the property .You can use MVVM and the ViewModel inheritance the class

public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   protected async void OnPropertyChanged([CallerMemberName] string propName = "")
   {
       await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.High,
           () =>
           {
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
           });
   }
}

And all the property the Xaml bind use oneway or twoway and in the property use OnPropertyChanged(); And if the time my code expception and say that Current is null

You can use

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
{
     //write your code
     //in OnPropertyChanged use PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
});

If you wrote the code in user control or a page that you can see the below code.

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
{
            //UI code here
});

The CoreDispatcherPriority can set Priority but you shouldnt set it to High ,see CoreDispatcherPriority



回答3:

I also found another possibility how to update the UI - at this moment I am updating progress bar by this way, but found you can pass other variables..

 var progress = new Progress<int>(i => _progress = i);
 progress.ProgressChanged += Pr_ProgressChanged;

then event:

  private void Pr_ProgressChanged(object sender, int e)
    {
        progressBar1.Value = progressBar1.Value + 1;
    }

Method with parametrs:

 public async Task GetWebServers(IProgress<int> progress) {
 //your code
}

Calling:

await Task.WhenAll(workers.Select(webServer => webServer.GetWebServers(progress))
       );