Returning Values from ObjectReadyEventHandler

74 views Asked by At

I'm writing a program that returns a list of users currently logged in a server.

Here's the code that does this:

private List<string> GetUsers(string server)
    {
        ConnectionOptions options = new ConnectionOptions();
        ManagementScope moScope = new ManagementScope(@"\\" + server + @"\root\cimv2");
        moScope.Connect();
        ObjectQuery query = new ObjectQuery("select * from Win32_Process where name='explorer.exe'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(moScope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject m in queryCollection)
        {
            ManagementOperationObserver mo = new ManagementOperationObserver();
            mo.ObjectReady += new ObjectReadyEventHandler(mo_ObjectReady);
            m.InvokeMethod(mo, "GetOwner", null);
        }


    }

    private void mo_ObjectReady(object sender, ObjectReadyEventArgs e)
    {
        string user = e.NewObject.Properties["user"].Value.ToString();
        //I would like to return the username back to the GetUsers function. How can I do this?
    }

The mo_ObjectReady can get one username that is currently logged in the server. How could I return the value to the GetUsers function so I can put them altogether in a List?

Thanks in advance.

0

There are 0 answers