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. Visual Basic
  4. API to get Application Status (running, stopped, etc) [modified]

API to get Application Status (running, stopped, etc) [modified]

Scheduled Pinned Locked Moved Visual Basic
data-structuresjsonhelpquestion
8 Posts 2 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.
  • M Offline
    M Offline
    Mitch F
    wrote on last edited by
    #1

    I am currently writing an application that launches and manipulates other programs. I currently launch another application through the use of the process class, and since I need to manage many other applications, I just store the MainWindowHandle value into an array, and dispose the process object that I had created. The problem that I am having is, when it comes time to manipulate the application, I do not know whether the application is still running or not(has the user closed it?). Is there a Windows API that returns values based on a window handle? Is there any alternative to using an API to accomplish this? Would I be better off creating an array of processes, eg Dim myArray() as Process ? Any suggestions are greatly appreciated

    modified on Monday, March 17, 2008 11:42 PM

    S 1 Reply Last reply
    0
    • M Mitch F

      I am currently writing an application that launches and manipulates other programs. I currently launch another application through the use of the process class, and since I need to manage many other applications, I just store the MainWindowHandle value into an array, and dispose the process object that I had created. The problem that I am having is, when it comes time to manipulate the application, I do not know whether the application is still running or not(has the user closed it?). Is there a Windows API that returns values based on a window handle? Is there any alternative to using an API to accomplish this? Would I be better off creating an array of processes, eg Dim myArray() as Process ? Any suggestions are greatly appreciated

      modified on Monday, March 17, 2008 11:42 PM

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      You could store the actual process object in an array or you can use the Process.GetProcessByName[^] method to find the process again. You would want to store the process name (the name of the executable) rather than the handle.

      Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


      [Forum Guidelines] [Articles] [Blog]

      M 1 Reply Last reply
      0
      • S Scott Dorman

        You could store the actual process object in an array or you can use the Process.GetProcessByName[^] method to find the process again. You would want to store the process name (the name of the executable) rather than the handle.

        Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


        [Forum Guidelines] [Articles] [Blog]

        M Offline
        M Offline
        Mitch F
        wrote on last edited by
        #3

        Thanks for the advice. Storing the process in a process array makes things a lot easier. Thanks, Mitch

        S 1 Reply Last reply
        0
        • M Mitch F

          Thanks for the advice. Storing the process in a process array makes things a lot easier. Thanks, Mitch

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          You're welcome. The one thing to keep in mind when doing that is going to be how long you must keep those references and how many of them you will be keeping. Holding a bunch of Process objects in memory in an array will consume a lot more memory than just holding a string value of the process name. If you won't need access to the Process information for a while (or are passing the array around as a function parameter) you really should think about just keeping the process name and only creating a Process object as needed.

          Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


          [Forum Guidelines] [Articles] [Blog]

          M 1 Reply Last reply
          0
          • S Scott Dorman

            You're welcome. The one thing to keep in mind when doing that is going to be how long you must keep those references and how many of them you will be keeping. Holding a bunch of Process objects in memory in an array will consume a lot more memory than just holding a string value of the process name. If you won't need access to the Process information for a while (or are passing the array around as a function parameter) you really should think about just keeping the process name and only creating a Process object as needed.

            Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


            [Forum Guidelines] [Articles] [Blog]

            M Offline
            M Offline
            Mitch F
            wrote on last edited by
            #5

            You are correct. When I changed out my process array for just a regular array, I had saved quite a bit of memory. I am also able to get the process by using the GetProcessById method. However, once I call theProcess.HasExited method, I get a "Win32 Exception: Access is Denied". [Edit] With a little more digging around in my code, it seems that my error is being caused earlier in my code. Dim newApp As Process = New Process newApp.StartInfo.FileName = appSelPath newApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal newApp.Start() However, when I call newApp.MainWindowHandle to store the value in the array (I need to use this for Windows API's), it returns 0. Thanks for your time, Mitch

            S 1 Reply Last reply
            0
            • M Mitch F

              You are correct. When I changed out my process array for just a regular array, I had saved quite a bit of memory. I am also able to get the process by using the GetProcessById method. However, once I call theProcess.HasExited method, I get a "Win32 Exception: Access is Denied". [Edit] With a little more digging around in my code, it seems that my error is being caused earlier in my code. Dim newApp As Process = New Process newApp.StartInfo.FileName = appSelPath newApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal newApp.Start() However, when I call newApp.MainWindowHandle to store the value in the array (I need to use this for Windows API's), it returns 0. Thanks for your time, Mitch

              S Offline
              S Offline
              Scott Dorman
              wrote on last edited by
              #6

              That is probably a better solution since you are the one starting the processes you will have access to the process Ids. There are situations where Process.HasExited will throw a Win32Exception so you will still want to trap for that error and handle it appropriately. This mostly occurs when the exit status can't be returned for some reason. As for MainWindowHandle returning 0, this is also a legal value. A process has a main window associated with it only if it has a graphical interface; otherwise MainWindowHandle will be 0. Take a look at WaitForInputHandle[^] to allow the process to finish startng.

              Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


              [Forum Guidelines] [Articles] [Blog]

              M 1 Reply Last reply
              0
              • S Scott Dorman

                That is probably a better solution since you are the one starting the processes you will have access to the process Ids. There are situations where Process.HasExited will throw a Win32Exception so you will still want to trap for that error and handle it appropriately. This mostly occurs when the exit status can't be returned for some reason. As for MainWindowHandle returning 0, this is also a legal value. A process has a main window associated with it only if it has a graphical interface; otherwise MainWindowHandle will be 0. Take a look at WaitForInputHandle[^] to allow the process to finish startng.

                Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


                [Forum Guidelines] [Articles] [Blog]

                M Offline
                M Offline
                Mitch F
                wrote on last edited by
                #7

                Thanks a lot for all of your help, it is greatly appreciated. Although, it seems that WaitForInputIdle does not want to work correctly for me. This is the code that I am now using: Dim newApp As Process = New Process newApp.StartInfo.FileName = appSelPath newApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal newApp.Start() newApp.WaitForInputIdle() MsgBox(newApp.MainWindowHandle.ToString) Even when I call WaitForInputIdle(), it does not stall my application and I immediately get 0 as the main window handle... It works with notepad.exe, but when I launch iexplorer.exe (internet explorer), I still get 0... Thanks, Mitch

                modified on Tuesday, March 18, 2008 3:11 PM

                S 1 Reply Last reply
                0
                • M Mitch F

                  Thanks a lot for all of your help, it is greatly appreciated. Although, it seems that WaitForInputIdle does not want to work correctly for me. This is the code that I am now using: Dim newApp As Process = New Process newApp.StartInfo.FileName = appSelPath newApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal newApp.Start() newApp.WaitForInputIdle() MsgBox(newApp.MainWindowHandle.ToString) Even when I call WaitForInputIdle(), it does not stall my application and I immediately get 0 as the main window handle... It works with notepad.exe, but when I launch iexplorer.exe (internet explorer), I still get 0... Thanks, Mitch

                  modified on Tuesday, March 18, 2008 3:11 PM

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  You're welcome. Glad to help out. It seems that WaitForInputIdle() applies only to process with a UI but there may also be other conditions which cause it to return false. You might want to consider uisng the WaitForInputIdle(Int32) overload since the version you are calling has the potential to wait forever. Also, if for some reason the process doesn't have a message loop, both overloads will immediately return false, so you might want to test the return value as well.

                  Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


                  [Forum Guidelines] [Articles] [Blog]

                  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