I currently use the following code to asynchronously create a cimsession to a remote host, query the remote host, return multiple values and finally updated some textboxes on my WPF window. Just wanting to know if anyone can see any glaring issues (I'm new to all things async and c# :) ) or a better was of doing it:
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Management.Infrastructure;
//creating cimsession to remote host
CimSession session = await Task.Run(() =>
{
return CimSession.Create(computerHostName);
});
//getting username, bootupstate, manufacturer and model
var GI1 = await Task.Run(() =>
{
var results = new List<string>();
IEnumerable<CimInstance> queryResults = session.QueryInstances(nameSpace, WQL, "SELECT Username, BootUpState, Manufacturer, Model FROM Win32_ComputerSystem");
foreach(CimInstance i in queryResults)
{
results.Add(i.CimInstanceProperties["Username"].Value.ToString());
results.Add(i.CimInstanceProperties["BootUpState"].Value.ToString());
results.Add(i.CimInstanceProperties["Manufacturer"].Value.ToString());
results.Add(i.CimInstanceProperties["Model"].Value.ToString());
}
//returning all the variables
return results;
});
//adding username, bootupstate, manufacturer and model to their textboxes
GIUS.Text = GI1[0];
GIBS.Text = GI1[1];
GIMF.Text = GI1[2];
GIMD.Text = GI1[3];
Any suggestions or help is appreciated :)