Example: Making Sure Only One Copy of an Application is Running
The Pega Robotic Automation Studio’s plug-in lets you work in the environment you prefer. You can still do automations graphically as before, but you can use VB.NET, C#, or any other Common Language Runtime (CLR) compliant language. There are, however, some things which the graphical automations and Pega Robotic Automation Runtime took care of for you that you do not get out of the box in Visual Studio. This includes:
- Built-in support for application bars
- The automatic preventing of multiple copies of your application from running
Note: Application bars are discussed in another article. This article shows a way to make sure only one copy of your application is running, if needed.
Most programs can have multiple copies running at a time, such as browsers, Microsoft Word, Visual Studio, and Pega Robotic Automation Studio. In business situations, however, you often need to prevent multiple copies of a program from running. In a call center, usually only one copy of the CRM system is available at a time to minimize mistakes. Frequently, a Studio solution that integrates multiple applications must also make sure only one instance of the solution is running.
Runtime does this quite easily using settings in the RuntimeConfig.xml file. By default, only one copy of Runtime can run at a time and this keeps you from having to do anything to prevent duplicate solutions from running. If you do want more than one copy of a solution or want multiple solutions, you change the SingleCopy setting to False in the configuration file.
There is no equivalent configuration setting in .NET, instead you do it with code. There are several ways to do this and two are described here:
- Create a mutex object which is visible by all programs on a system. The mutex is given a name to identify. You can use any name. The name used here is the name of the executable program including its path. The code can then detect if the same executable is run twice and, if you need to run the program twice, you can just copy it to another directory and run it from there.
- You do not need to use a mutex. You can use any globally visible named object such as a named pipe to accomplish this.
///The following ensures only one copy of this program is running at a time.
/// true if we are the initial instance
public static bool checkIfInitialInstance()
bool requestInitialOwnership = true;
// This names the mutex but also permits multiple programs to run if they are
// started from different directories. Need to first replace directory path characters
// that isn’t permitted in a name.
string mutexName = Application.StartupPath.Replace( '\\', '_' );
mutexName = mutexName.Replace( ':', '_' );
// Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "mutexName" can exist. If "mutexName" is created by this call,
// then mutexWasCreated is true; otherwise, it is false.
singleInstanceMutex = new System.Threading.Mutex( requestInitialOwnership, mutexName, out mutexWasCreated );
// This thread owns the mutex only if it both requested
// initial ownership and created the named mutex.
if ( !( requestInitialOwnership && mutexWasCreated ) )
return false; // this is the only instance running
return true; // there is another instance running, do something about it like <em>Application.Exit()</em>
When using Citrix
You need to allow more than one copy of the application to run if you are working with Citrix, yet you still need to prevent a single user from running it more than once. This is because each user on the Citrix box may be running the same program but in his or her own environment. The mutex approach, however, would prevent this.
To allow a program to run once for each user in a Citrix (or Windows terminal server) environment, add this code to the naming of the mutex before creating it.
This makes the mutex unique for the user and program rather than just the program.