简体   繁体   中英

Adding background threads to a WPF Prism MVVM Application

Is there a recommended way or possibly a guide on implementing background threading in a WPF Prism 6 application? I need to make several WMI calls to hundreds of remote computers and displaying the status and WMI data to the GUI. I've followed this Prism 6 video to get just about everything working with the exception of the threading.

I'm not sure that's specific to Prism, but to start hundreds of asyncrhonous calls I'd use tasks:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM