VB.NEt instance checking app
-
I am trying to figure out if VB.NET (or even VB6) is the best way to test for an existing instance of an application. Basically, I want the code to test (I assume in Processes) whether the application is already active, and if it is, not open a second instance. (Of course, if it is not already running, then the application should open). Can anyone steer me in the right direction?
-
I am trying to figure out if VB.NET (or even VB6) is the best way to test for an existing instance of an application. Basically, I want the code to test (I assume in Processes) whether the application is already active, and if it is, not open a second instance. (Of course, if it is not already running, then the application should open). Can anyone steer me in the right direction?
Your application should do this, not some external application. The way you've worded this suggests that you think otherwise. No, C++ is the best language to do this in, but if you're stuck with VB.NET, then there's no reason why VB.NET can't do it. There are tons of examples on the web, and probably even on CP. You're more likely to find them in C#, I'd have thought, but they should be easy to convert. I have no doubt that this is beyond VB6's meagre abilities. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
Your application should do this, not some external application. The way you've worded this suggests that you think otherwise. No, C++ is the best language to do this in, but if you're stuck with VB.NET, then there's no reason why VB.NET can't do it. There are tons of examples on the web, and probably even on CP. You're more likely to find them in C#, I'd have thought, but they should be easy to convert. I have no doubt that this is beyond VB6's meagre abilities. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
Yes, the base application should do this...long story as to why it doesn't. Anyway, VB.NET is what I'm "stuck with" to achieve this objective. Can you point me to some examples "on the web" and/or CodeProject? Thanks!
You can do this with API calls, which are even within the grasp of VB6 "meager" abilities. LOL. EnumProcesses is one way but it can be cumbersome to work with. If the application creates a main window that has a reasonably unique caption that does not change (in other words, this would never work with a web browser, but is feasible for most regular compiled apps) you can use the FindWindowEx API. If you search for the app caption and window class, and the handle returns as zero, the app is (probably) not running. Use Spy to help you determine the caption and window class name to search for. Robert
-
You can do this with API calls, which are even within the grasp of VB6 "meager" abilities. LOL. EnumProcesses is one way but it can be cumbersome to work with. If the application creates a main window that has a reasonably unique caption that does not change (in other words, this would never work with a web browser, but is feasible for most regular compiled apps) you can use the FindWindowEx API. If you search for the app caption and window class, and the handle returns as zero, the app is (probably) not running. Use Spy to help you determine the caption and window class name to search for. Robert
-
Robert, Thank you for the information. I am not familiar with the type of calls you are referring to. Could you provide a code example to accomplish what you are describing? Thanks!
Here is a quick and dirty way of checking for a process. First you need a reference to the SystemDiagnostics.
Imports System.Diagnostics
ThenDim myProcess As Process() = Process.GetProcessesByName("ProcessName") If myProcess.Length > 1 Then MsgBox("You can only have one instance of this application running.") End If
Hope this helps :-)