DOWNLOAD THE CODE:
Download the Code 37748.zip

Users sometimes become impatient when they're trying to launch an application. While the application is loading, a user might double-click the icon again and launch a second instance of the application. When you're running Windows 2000 Server Terminal Services—as in my organization—launching multiple instances of applications negatively affects performance.

To prevent users from launching a second instance of an application, I wrote the script that Listing 4 shows. ChkProcess.vbs checks to determine whether a user is already running an application. If so, the script generates a message box that tells the user he or she can run only one instance of the application at a time. The script uses the command-line arguments Program and Target. The script's syntax is wscript chkprocess.vbs app.exe c:\appfolder\app.exe.

End of Article




You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Thanks for the script, David! It works, and it’s a great learning tool. I've wanted to learn how to work with VBScript, and I found your script to be highly informative. I have some decent experience with MS-DOS style batch files, and experience with using Visual Basic for Applications (VBA) within Microsoft Access. However, this is the first time that I've worked with a VBScript file. Thanks for taking the time to provide the information.

I also wanted to offer a couple of tips for anyone else that's new to scripting. First, if you download the code (37748.zip) and extract the ZIP file, you will end up with a file called Listing_04.ChkProcess.txt. You can rename this file to ChkProcess.VBS. Then you need to edit the file in NotePad and either delete or "rem out" the first line: LISTING 4: ChkProcess.VBS Otherwise you will get an "Expected Statement" error when running the script. To "rem out" the line, stick a single quote character (') at the beginning of the line. Again, you can also delete the line altogether because it's not part of the required code.

A second tip that I want to offer is that one of the lines in this file had a carriage return <CR> inserted into the middle of the line, which broke it into two lines instead of one. You need to remove this <CR> or you will get an "Unterminated string constant" error when you run the script.

The two lines in question are: MsgBox user & " is already running " & Program & " ... You cannot open another instance",vbexclamation,"Warning" You need to remove the <CR> so that you are left with a single line: MsgBox user & " is already running " & Program & " ... You cannot open another instance",vbexclamation,"Warning"

Once you've completed the two tips that I mentioned, the script will run without any errors.

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5

Hello, again! I wanted to offer another tip.

The script in its current form is case-sensitive when you are typing the "app.exe" parameter. This parameter specifies the name of the process as it appears within Task Manager. Most process names are listed in lower case. Occasionally, you'll get a process name that's listed in all upper case or mixed case. A good example is Microsoft Word 2003, which shows up in the process list as "WINWORD.EXE" instead of "winword.exe". You must specify the "app.exe" parameter exactly as it appears in the process list (including the case of the letters) in order for the script to work correctly.

Now I want to share a "tweak". If you change the following line, you don't have to worry about the case-sensitivity issue: From: If proc.name = Program then To: If UCase(proc.name) = UCase(Program) then

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5

Hello, again! I have a minor problem that I haven’t resolved yet.

I've found that if the user tries to load two copies of a program too quickly, the script will still let both instances load. For example, if a user has a shortcut on the QuickLaunch Bar, and then double-clicks on this shortcut instead of single-clicking on it, two instances of the program will still load. Presumably, this is because when the script for the second instance checks for the process name, the process name isn't found because the first instance of the program hasn't finished loading yet. I get the same result if the user double-clicks on a shortcut on the desktop and then double-clicks on the same shortcut a second time right away.

I'm using this script in a Windows Server 2003 Service Pack 1 Terminal Services environment, whereas David uses it in a Windows 2000 Server Terminal Services environment. There may be a difference between these two operating systems that affects the timing of process loading.

Anyway, does anyone have any ideas for a solution?

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5

I apologize, but the facility to enter comments has stripped out a lot of tabs and carriage returns in my previous comments. This makes it very hard to read the lines of code that I was referring to.

I my first comment, I referred to deleting or “remming out” the first line of code. The line of code that I was referring to is as follows:

LISTING 4: ChkProcess.VBS

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5

I apologize, but the facility to enter comments has stripped out a lot of tabs and carriage returns in my previous comments. This makes it very hard to read the lines of code that I was referring to.

In my first comment, I referred to removing an extraneous carriage return that was inserted into the script file. The line of code that I was referring to is as follows:

MsgBox user & " is already running " & Program & " ... You cannot open another instance",vbexclamation,"Warning"

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5

I apologize, but the facility to enter comments has stripped out a lot of tabs and carriage returns in my previous comments. This makes it very hard to read the lines of code that I was referring to.

In my second comment, I referred to changing a line to resolve a case-sensitivity issue. The original line of code to which I was referred to is as follows:

If proc.name = Program then

The modified line of code is as follows:

If UCase(proc.name) = UCase(Program) then

Thanks, Van

van@vangor.net

vangor541

Article Rating 5 out of 5