任务中的UWP更新UI [英] UWP update UI from Task

查看:104
本文介绍了任务中的UWP更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它正在检查本地网络中的网络范围(用于运行http服务)。

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

这意味着,我正在检查f.e。从10.0.0.1到10.0.0.255。这就是问题所在,在PC上运行时速度足够,但是在Lumia 950上运行时速度不够。因此,我想在扫描期间更新用户界面。

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.

所以这是问题:


  1. 此刻,我有几个任务10-每个任务都在扫描他的射程f.e.任务1-10.0.0.1到10.0.0.25等。-我应该使用10个任务还是有某种方法,.net本身将如何解决?表现如何如果我要使用50个任务?

  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?

第二个问题是,在扫描过程中,我会找到PC,Web服务在其中运行,但是...如何找到PC后应该更新UI吗?目前,当所有任务完成时,我只能这样做。

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

推荐答案

我还发现了另一种如何更新UI的可能性-目前,我正在通过这种方式更新进度栏,但发现您可以传递其他变量。

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;

然后事件:

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

带有参数的方法:

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

调用:

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

这篇关于任务中的UWP更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆