Process.GetProcesses(userName) throws InvalidOperationException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeMann
    New Member
    • Jun 2014
    • 6

    Process.GetProcesses(userName) throws InvalidOperationException

    Hello all. I came across a posting that addressed exactly what I'm trying to accomplish, i.e. "to get a list of Processes that have a window" by userName, which I found at http://bytes.com/topic/c-sharp/answe...if-app-running.

    I'm writing this in C-Sharp in Visual Studio 2010 and am testing it on a Windows Vista 32-bit system, but the final target will be a Windows 7 64-bit system.

    I want to get a list of running processes that have a window, i.e. what would show up in TaskManager and that are started by the user. My intention is to do a process.Kill() on certain processes.

    The code example shown in the URL above suggested acquiring the local users userName from as follows:
    Code:
    string userName = Environment.UserName;
    Process[] procList = Process.GetProcesses(userName);
    for (int i = 0; i < procList.Length; i++)
    {
        if (procList[i].MainWindowHandle != IntPtr.Zero)
        {
            listBox1.Items.Add(procList[i].ProcessName + "\t" +
                procList[i].MainWindowTitle);
        }
    }
    Note: in testing the code I replaced ArrayList with a simple winform listbox, although the final code will be placed in a Form_Deactivate event to run automatically.

    While userName returns the current user name, the problem I've experienced is that plugging in userName to get the processes throws an InvalidOperatio nException with the additional details of "Couldn't connect to remote machine".

    Please note that this is a standalone computer not connected to any network and neither will the Windows 7 computer be on any network.

    I read comments on another forum indicating that there are 2-flavors of Process.GetProc esses and to leave out the userName. However, trying that not only pulls back all the running processes on the system, those with a window and those that do not; but then it also throws a Win32Exception.

    I want to get only those processes started by the user which have a window. Does anyone know how to get around the InvalidOperatio nException being thrown? The example in the aforementioned URL appears to be what I'm after, but won't run without this error. Thank you
    Last edited by Niheel; Jun 13 '14, 08:05 PM. Reason: adding clickable link to reference question
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    This is a little sloppy since I'm not a C# developer, but I actually use something very similar to this in VB.NET so I just mildly converted it for you. I tested it and it works fine.

    Code:
            {
                Process[] myProcesses = Process.GetProcesses();
    
                foreach (Process processWithWindow in myProcesses)
                {
                    if (processWithWindow.MainWindowTitle != "")
                    {
                        listBox1.Items.Add(processWithWindow.MainWindowTitle + "  -  " + processWithWindow.ProcessName);
                    }
                }
            }

    Comment

    • CodeMann
      New Member
      • Jun 2014
      • 6

      #3
      Hello Luk3r and thanks for your reply. It does give me a list of running processes having a window without throwing the aforementioned exceptions.

      Curiously though, it did not show a Windows Explorer window that i had open, whereas TaskManager does show that window along with the others.

      Not sure why it excluded that window. Any ideas why that window wouldn't show up in the list?
      Thanks.

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        Excellent question. My best guess is: Likely because Windows Explorer is launched from the explorer.exe process rather than running a new process. I find it hard to imagine you'd ever want to kill the "explorer.e xe" process to close the Windows Explorer window :)

        Comment

        • CodeMann
          New Member
          • Jun 2014
          • 6

          #5
          Hello Luk3r, you are correct, I wouldn't want to kill explorer.exe, so that window wouldn't be a problem.

          Interestingly though, the Win32Exception returned, with an exception message of "Access is denied" and this is on my own system, which has Administrator rights.

          I can't imagine that I would need elevated rights on my own box.
          Any thoughts on why I'm getting this, or perhaps more importantly, how to get around this exception, as my application is going to be ported over to another person's Windows 7 machine?

          Thanks again.

          Comment

          • CodeMann
            New Member
            • Jun 2014
            • 6

            #6
            Also, to note, this Win32Exception occurred when I attempted the
            Code:
            process.Kill()

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              Can you please post your new code (the code including the process.Kill)?

              Comment

              • CodeMann
                New Member
                • Jun 2014
                • 6

                #8
                Hello Luk3r, sorry I didn't post this sooner. It's entirely possible that I don't have the syntax correct on this Kill function. I've used a process.Kill before when I was looking to stop one specific process and it worked fine.

                However, what I'm trying to do here is kill any process with a window that doesn't correspond to my running application.

                When I debugged my code, I found that the list of processes being returned from procList had every process on the system, not just those with a window, i.e. background processes as well, which I do not want to kill.

                Here is a sample of my code, where I first excluded the processes of my application in a "switch" statement, then in the default statement I want to do the Kill.

                Code:
                public void getProcesses()
                {
                    try
                    {
                        Process[] procList = Process.GetProcesses();
                                                    
                        foreach (Process process in procList)
                        {
                            switch (process.MainWindowTitle + " - " + process.ProcessName)
                            {
                                case "Specific  App in VS - Microsoft Visual Studio - devenv":
                                case "Specific  App (Running) - Microsoft Visual Studio - devenv":
                		case "Specific App already deployed - The Specific App name":
                                    break;
                                default:
                                      process.Kill(); //anything other than the currently running Specific App
                                      break;
                            }
                        }
                    }
                    catch (Win32Exception) { }
                    catch (NotSupportedException) { }
                    catch (InvalidOperationException) { }
                }

                Comment

                • CodeMann
                  New Member
                  • Jun 2014
                  • 6

                  #9
                  Hello Luk3r, well, as I sometimes like to say, "the littlest things can cause the biggest problems." I had originally created a mockup using your code suggestion, using a simple form with a list-box and button, which worked great.

                  I then attempted to port this over to my main body of code in the real application and completely forgot to include the check if the MainWindowTitle was empty and wondered why procList had every background process in the system and not just those with a window. Oh well. :-(

                  When I included that line in my code, it works fine. (scrape egg off face here). Therefore, your code suggestion solved the problem I was working on and I give you a big Kudos for thinking to check the MainWindowTitle .

                  Many thanks for your help.

                  Comment

                  • Luk3r
                    Contributor
                    • Jan 2014
                    • 300

                    #10
                    Sorry for the late reply. Busy weekend! Glad you got it all figured out and happy coding! :)

                    Comment

                    Working...