Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. System.Diagnostics.Process.GetProcessesByName Question

System.Diagnostics.Process.GetProcessesByName Question

Scheduled Pinned Locked Moved .NET (Core and Framework)
questiondata-structuresannouncement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gmhanna
    wrote on last edited by
    #1

    Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?

    Process[] pProcess;
    pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");

    Thank you,

    Glenn

    N _ 2 Replies Last reply
    0
    • G gmhanna

      Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?

      Process[] pProcess;
      pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");

      Thank you,

      Glenn

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Would it not be better to use the Office Object Model and open the workbooks from Excel? You would have more control over the process that way.


      I know the language. I've read a book. - _Madmatt

      Brian C HartB 1 Reply Last reply
      0
      • N Not Active

        Would it not be better to use the Office Object Model and open the workbooks from Excel? You would have more control over the process that way.


        I know the language. I've read a book. - _Madmatt

        Brian C HartB Offline
        Brian C HartB Offline
        Brian C Hart
        wrote on last edited by
        #3

        Only problem is, even if you make sure and call System.InteropServics.Runtime.Marshal.ReleaseComObject() on each and every little object you access with the object model, and call all the cleanup functions of the object correctly, one still finds little EXCEL.EXE's hiding around when you open up Task Manager. I think it's maybe a COM Subsystem thing, where the system likes to leave the server EXE hanging around when you use COM objects from it -- even if they're properly released -- so that there's less latency the next time you make COM method calls from that same EXE. However, if you're cleaning up Excel every time your application is run and you don't want to have these little orphan instances of EXCEL.EXE hanging around, well, then you want to add this little piece of code back in to your cleanup method:

        foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("excel.exe"))
        proc.Kill();

        It's not immediately obvious what order the processes come out in. I guess you'll just have to think of a way you can inspect each entry. In the meantime, though, foreach does in a pinch.

        Sincerely Yours, Brian Hart

        P 1 Reply Last reply
        0
        • Brian C HartB Brian C Hart

          Only problem is, even if you make sure and call System.InteropServics.Runtime.Marshal.ReleaseComObject() on each and every little object you access with the object model, and call all the cleanup functions of the object correctly, one still finds little EXCEL.EXE's hiding around when you open up Task Manager. I think it's maybe a COM Subsystem thing, where the system likes to leave the server EXE hanging around when you use COM objects from it -- even if they're properly released -- so that there's less latency the next time you make COM method calls from that same EXE. However, if you're cleaning up Excel every time your application is run and you don't want to have these little orphan instances of EXCEL.EXE hanging around, well, then you want to add this little piece of code back in to your cleanup method:

          foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("excel.exe"))
          proc.Kill();

          It's not immediately obvious what order the processes come out in. I guess you'll just have to think of a way you can inspect each entry. In the meantime, though, foreach does in a pinch.

          Sincerely Yours, Brian Hart

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I suspect that Mark isn't going to have to think too hard on this, but the OP might instead.

          I'm not a stalker, I just know things. Oh by the way, you're out of milk.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • G gmhanna

            Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?

            Process[] pProcess;
            pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");

            Thank you,

            Glenn

            _ Offline
            _ Offline
            _Erik_
            wrote on last edited by
            #5

            Push your processes into a Stack and kill them when you're done in the same order you pop them from the Stack.

            G 1 Reply Last reply
            0
            • _ _Erik_

              Push your processes into a Stack and kill them when you're done in the same order you pop them from the Stack.

              G Offline
              G Offline
              gmhanna
              wrote on last edited by
              #6

              Actually a better method is to use the COM Excel application Quit method. You can also set the DisplayAlerts property to true or false depending if you want to get prompted for unsaved files. Thank you for your input.

              Glenn

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups