UWP update user interface from Task

I have an application that checks network ranges (for starting an http service) on a local network.

So this means that I am checking fe from 10.0.0.1 to 10.0.0.255. And here is the problem: when working on a PC, the speed is enough, but when working on the Lumia 950, the speed is not enough. Therefore, I would like to update the interface during the scan.

So here are the questions:

  • At this moment, I have several tasks, i.e. 10 - each task scans its range fe task 1 - 10.0.0.1 to 10.0.0.25, etc. - should I use 10 tasks or is there some way how .net will solve it myself? What will be the performance, i.e. if i will use 50 tasks?

  • Second question: during the scan, I will find a PC running the web service, but ... How can I update the interface when a PC is detected? At this point, I can only do this after completing all the tasks.

The methods that I call are async Tasks

+4
source share
3 answers

I also found another way to update the user interface - at the moment I am updating the progress bar in this way, but found that you can pass other variables ..

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

that event:

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

Method with parameters:

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

Call:

await Task.WhenAll(workers.Select(webServer => webServer.GetWebServers(progress))
       );
0
source
  • , , . .

  • , :

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

, . MVVM ViewModel

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 the whole property that Xaml binds uses oneway or twoway, and the property uses OnPropertyChanged (); And if the time of my calculus code and say thatCurrent 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 a user control or the page on which you see the code below.

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

CoreDispatcherPriority can set priority, but you should not set it to High, see CoreDispatcherPriority

+4
source

All Articles