Skip to content

Instantly share code, notes, and snippets.

@IT-Delinquent
Created July 27, 2021 11:30
Show Gist options
  • Select an option

  • Save IT-Delinquent/409a4300834e74fc12ee3add64680744 to your computer and use it in GitHub Desktop.

Select an option

Save IT-Delinquent/409a4300834e74fc12ee3add64680744 to your computer and use it in GitHub Desktop.
cSharpAsyncWatcherClass
//Used to define what is returned in the async results
public static CimAsyncMultipleResults<CimInstance> GetValues(CimSession _session) {
return _session.QueryInstancesAsync(@"root\cimv2", "WQL", "SELECT Username FROM Win32_ComputerSystem");
}
//This watches the async progress
class CimInstanceWatcher : IObserver<CimInstance> {
public void OnCompleted() {
Console.WriteLine("Done");
}
public void OnError(Exception e) {
Console.WriteLine("Error: " + e.Message);
}
public void OnNext (CimInstance value) {
Console.WriteLine("Value: " + value);
}
}
private static void Main() {
//Leaving cimsession creation as sync because is happens "instantly"
CimSession Session = CimSession.Create("PC-NAME");
//Creating a new watcher object
var instanceObject = new CimInstanceWatcher();
//Subscribing the watcher object to the async call
GetValues(Session).Subscribe(instanceObject); Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment