C# Tasks for Asynchronous Operations 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(() =&gt;
{
     return CimSession.Create(computerHostName);
});

//getting username, bootupstate, manufacturer and model 
var GI1 = await Task.Run(() =&gt;
{
    var results = new List&lt;string&gt;();
    IEnumerable&lt;CimInstance&gt; 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 :)
//creating cimsession to remote host
CimSession session = await Task.Run(() =&gt;
{
     return CimSession.Create(computerHostName);
});



Task.Run is a red flag. This should be

CimSession session = await CimSession.CreateAsync(computerHostName);


Similarly for every method you call on session which has a ...Async version, you should use the ...Async version.



It seems that you're calling this from a GUI thread. That means you need to be careful about where your continuations are executed. I would suggest pulling the async calls out into a method and using .ConfigureAwait(false) on all of them, and then calling that method from the GUI thread with ContinueWith(...) to update the GUI, but since you refuse to provide more context I can't be more specific.