【发布时间】:2014-08-06 02:38:27
【问题描述】:
点网版本 = 4。 语言 = C#
我几乎是 c# 的新手,所以请把我当作初学者:)。
我想做什么: 创建一个在数据网格中显示计算机正常运行时间信息的 WPF 应用程序 从列表框中获取计算机名并使用 TPL 运行“Ping”函数,该函数返回一个状态属性,指示计算机是处于活动状态还是无法访问。
如果任务的状态属性返回“成功”,那么我将运行“正常运行时间”函数以从服务器获取正常运行时间并将其添加到可观察的集合中,该集合将用作数据网格的输入。
我目前无法运行第二个任务,即“正常运行时间”功能,它似乎冻结了 UI。我不确定我是否应该在“continuewith”块内运行“uptime”功能。 此外,我希望能够在需要时取消所有操作。
代码:
private void btn_Uptime_Click(object sender, RoutedEventArgs e)
{
List<Task> tasks = new List<Task>();
tokenSource = new CancellationTokenSource();
var ui = TaskScheduler.FromCurrentSynchronizationContext();
foreach (var item in Listbox1.Items)
{
string host = item.ToString();
// Start Parallel execution of tasks
var compute = Task.Factory.StartNew(() =>
{
return PingHost(host);
}, tokenSource.Token);
tasks.Add(compute);
if(compute.Result.Status.ToLower() == "success")
{
//Need to run the getuptime function based on the "success" result from each compute task
//without freeqing the UI.
WMIHelper.GetUptime(compute.Result.ComputerName);
}
}
【问题讨论】:
标签: wpf c#-4.0 task-parallel-library