I have the following function to fetch process information into a DataTable:

```
public DataTable getProcesses()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Path");
            dt.Columns.Add("User");
            dt.Columns.Add("Priority");

            string pid = "-";
            string name = "-";
            string path = "-";
            string priort = "-";
            string user = "-";

            string query = "Select * From Win32_Process";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();

            foreach (ManagementObject proc in processList)
            {
                pid = proc["ProcessID"].ToString();
                name = proc["Name"].ToString();

                if (proc["ExecutablePath"] != null)
                {
                    path = proc["ExecutablePath"].ToString();
                    priort = proc["Priority"].ToString();
                }
                string[] argList = new string[2];
                int returnVal = Convert.ToInt32(proc.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {
                    // return DOMAIN\user
                    user = argList[1] + "\\" + argList[0];
                }

                dt.Rows.Add(pid, name, path, user, priort);
            }

            return dt;
        }

```

This ultimately works and gives me the return I want, but it takes 20-30 seconds to execute. 

I was wondering if there's any way to optimize the function, or the query specifically, which is most likely the source of the "delay".

**edit**

After doing some of the things suggested in the comments, it has come down to an average of 15-20 seconds, but that's still way too long. At most, 4-5 seconds would be tolerable. I still haven't changed the query, is there anything I can do to make it faster?

**edit 2** 

After applying some of the changes suggested by @NicoRiff, and still getting the same runtime, I did some debugging to see what was effectively taking so long. Turns out it's one line in specific: `int returnVal = Convert.ToInt32(proc.InvokeMethod("GetOwner", argList));`, which will get me the user that "owns" each process.

This line takes about 60ms while all the others take 1-2ms. Over 200 iterations (which is the number of processes I have, I can only imagine the time it would take with a bigger list), it takes up about 12-13 seconds total (just for that one line), giving the 15-20 total overall.

Now that I've "singled-out" the problem, how can I optimize that function?