none
How to use CimSession async methods RRS feed

  • Question

  • Hi, 

    I've been banging my head against a brick wall for a long time trying to figure out how to use the async methods of Cimsession.CreateAsync and session.QueryInstanceAsync.

    I think I've managed to figure out how to use CimSession.CreateAsync with the below code, so if anyone can spot any glaring issues it would be much appreciated :) 

    public static CimAsyncResult<CimSession> CreateAsync(string computername)
    {
        return CimSession.CreateAsync(computername);
    }

    But I'm still stuck on using QueryInstancesAsync. Using the below code and I am getting the following error:

    public static CimAsyncMultipleResults<CimInstance> GetValues(CimSession _session) { return _session.QueryInstancesAsync(@"root\cimv2", "WQL", "SELECT Username FROM Win32_ComputerSystem"); }

    foreach (CimInstance i in GetValues(Session))

    {

    Console.WriteLine(i.CimInstanceProperties["Username"].Value

    }

    Error CS1579:

    foreach statement cannot operate on variables of type 'CimAsyncMultipleResults<CimInstance>' because 'CimAsyncMultipleResults<CimInstance>' does not contain a public instance definition for 'GetEnumerator'


    Thursday, June 27, 2019 1:19 PM

Answers

  • It seems that you have to do this:

       var myObject = new MyClass();

       GetValues(Session).Subscribe(myObject);

     

    where MyClass is like this:

     

       class MyClass : IObserver<CimInstance>

       {

          public void OnCompleted()

          {

             // . . .

          }

     

          public void OnError(Exception e)

          {

             // . . .

          }

     

          public void OnNext( CimInstance value )

          {

             // . . .

          }

       }

     

    The system will call OnNext periodically sending the available data. See the documentation for Subscribe.

    In case of forms, instead of MyClass you can also write ‘GetValues(Session).Subscribe(this)’ and implement the IObserver interface inside the form.

    Thursday, June 27, 2019 7:42 PM

All replies

  • Hello,

    Try setting var sessionList = GetValues(Session).ToList() then iterate sessionList.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 27, 2019 1:34 PM
    Moderator
  • Hi Kareninstructor,

    thank you for your quick reply. After editing the code to incorporate your suggestion, I get this error:

    Error CS1061:

    'CimAsyncMultipleResults<CimInstance>' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'CimAsyncMultipleResults<CimInstance>' could be found (are you missing a using directive or an assembly reference?) WMITesting

    Thursday, June 27, 2019 1:41 PM
  • It seems that you have to do this:

       var myObject = new MyClass();

       GetValues(Session).Subscribe(myObject);

     

    where MyClass is like this:

     

       class MyClass : IObserver<CimInstance>

       {

          public void OnCompleted()

          {

             // . . .

          }

     

          public void OnError(Exception e)

          {

             // . . .

          }

     

          public void OnNext( CimInstance value )

          {

             // . . .

          }

       }

     

    The system will call OnNext periodically sending the available data. See the documentation for Subscribe.

    In case of forms, instead of MyClass you can also write ‘GetValues(Session).Subscribe(this)’ and implement the IObserver interface inside the form.

    Thursday, June 27, 2019 7:42 PM
  • Hi I,T Delinquent,

    Thank you for posting here.

    Based on your description, I want to confirm some questions with you.

    How do you get the Session in the following code?

        foreach (CimInstance i in GetValues(session))

    I noted that CimSession.CreateAsync method will return CimAsyncResult<CimSession> type. Then, what is the usage of  CimSession.CreateAsync here?

    I hope that you could tell me the above information.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 28, 2019 3:25 AM
    Moderator
  • Hi Viorel_,

    thank you for your answer, its really the best I've had on the subject. I've managed to implement it in my testing project which I've put in below :)

    Is there any way to tell what value was returned by the CimInstanceWatcher? All I get is the WMI query and the hostname of the computer I am querying

    public static CimAsyncMultipleResults<CimInstance> GetValues(CimSession _session)
    {
    	return _session.QueryInstancesAsync(@"root\cimv2", "WQL", "SELECT Username FROM Win32_ComputerSystem");
    }
    
    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 creatio as sync because is happens "instantly"
    	CimSession Session = CimSession.Create("PC-NAME");
    	var instanceObject = new CimInstanceWatcher();
    	GetValues(Session).Subscribe(instanceObject);
    	Console.ReadLine();
    }


    Friday, June 28, 2019 8:37 AM
  • Hi Jack,

    Apologies for this, I was using

    CimSession Session = CimSession.Create("PC-NAME");
    
    in my test code. I am no longer using the asynchronous version of creating a cimsession since its done "instantly" anyway :) Apologies again for any confusion. 

    Friday, June 28, 2019 8:39 AM