|
The release of OpenSpan’s Visual Studio plug-in lets coder’s work in the environment they prefer. You can still do automations graphically as before but you can now use VB.Net or C# (or any other CLR compliant language). I prefer writing code against adapters and this is a great feature to provide.
However, there are some things which the graphical automations and OpenSpan Integrator took care of for you that you don’t get out of the box in Visual Studio.
Two of these are built-in support for application bars and automatic preventing of multiple copies of your application from running. Application bars are discussed in another article; this one shows a way to ensure only one copy of your application is running, if needed.
Most programs can have multiple copies run at a time, such as browsers, Microsoft Word, Visual Studio, OpenSpan Studio, you get the idea.
However in business situations we 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 so agents won’t make mistakes. Frequently an OpenSpan solution that integrates multiple applications must also ensure that only one instance of the solution is running.
OpenSpan Integrator does this quite easily using settings in its IntegratorConfig.xml file. By default only one copy of Integrator 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 we do it with code. There are dozens of ways to do this. To that I have used are described here.
The first one is to 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.
By the way, you don’t need to use a mutex. You can use any globally visible named object such as a named pipe to accomplish this.
private static System.Threading.Mutex singleInstanceMutex = null;
/// 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; bool mutexWasCreated;
// 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 Application.Exit() }
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 their own environment. However the mutex from above would prevent this.
To allow a program to run once for each user in a Citrix (or Windows terminal Server) environment, add the following code to the naming of the mutex before creating it.
mutexName = mutexName + “_” + System.Environment.UserName;
This makes the mutex unique for the user and program rather than just the program.
|