将后台线程添加到WPF Prism MVVM应用程序

时间:2015-08-27 00:12:19

标签: c# wpf multithreading mvvm prism

在WPF Prism 6应用程序中是否有推荐的方法或可能的指南来实现后台线程?我需要对数百台远程计算机进行多次WMI调用,并向GUI显示状态和WMI数据。我已经按照这个Prism 6 video来获得除了线程之外的所有工作。

1 个答案:

答案 0 :(得分:0)

我不确定这是针对Prism的,但是为了启动数百次异步调用,我会使用任务:

void DoWMICall(string computer)
{
    Console.WriteLine("Calling {0}", computer);
    Task.Delay(1000).Wait();
    Console.WriteLine("Computer {0} is OK", computer);
}

void CallComputers()
{
    var tasks = new Task[100];
    var computerNames = new string[100];
    for (int i = 0; i < 100; ++i)
    {
        computerNames[i] = Guid.NewGuid().ToString();
    }
    for (int i = 0; i < 100; ++i)
    {
        var index = i;
        tasks[index] = Task.Run(() => DoWMICall(computerNames[index]));
    }
    // This call blocks until all tasks are finished.
    Task.WaitAll(tasks);
}