API to get Application Status (running, stopped, etc) [modified]
-
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
-
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
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]
-
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]
-
Thanks for the advice. Storing the process in a process array makes things a lot easier. Thanks, Mitch
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]
-
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]
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
-
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
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]
-
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]
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
-
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
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]