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

查看:36
本文介绍了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上运行时速度不够.所以我想在扫描期间更新 UI.

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?

第二个问题是,在扫描过程中,我会找到正在运行 Web 服务的 PC,但是...当找到 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天全站免登陆