471,319 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Limit App to 1 Instance (Per File)?

I have a C# app that runs script files. The app is launched using a
command line argument to indicate which script should run. Most
scripts are run indefinitely in a loop and periodically perform tasks.
When the script is launched the Title Bar of the app indicates the name
of the app and the name of the script being run.

I thought to use a GetProcessesByName to return all instances of the
app and then to check the MainWindowTitle of each process to identify
that a script is running more than one instance. This almost works.
When the app is minimized, it runs in the system tray and the
MainWindowTitle returns and empty string.

While it is not difficult to prevent multiple instances of the app from
launching this is not the desired effect. The app may kick off any
number of instances. What matters is that the same script file is not
running under more than one instance of the app.

I realize I could record state information in a database or in a file
and check it upon launching the app, but there are timeliness issues
related to this approach -- the app may be too involved doing other
tasks to take the necessary time to update the state information for at
least several moments. Therefore, this approach is not
up-to-the-second accurate.

I considered checking scripts in and out. However, an abnormal
termination of the app may not properly check in a script as available.

Any ideas for making sure that only one instance of the app is running
any given script file?

Mario T. Lanza
Clarity Information Architecture, Inc.
2005.11

Nov 28 '05 #1
4 1294
Mutex' are used across application boundaries. Right before running a
script, make sure you can create a mutex for it with a valid return.
When the script is complete, close the Mutex.

Something like this:

bool okToRun;
Mutex m = new Mutex(true, myScriptName, out okToRun);
if (okToRun)
{
// run script here
m.Close();
m = null;
}

-Jerry

Nov 28 '05 #2
This is the first time I've heard anything about a "Mutex." I've just
now looked briefly into it and it seems like the right solution.
Thanks.

Nov 29 '05 #3
You may want to also append the app name to "myScriptName" just to make
sure it's unique (incase a script has a name that is the same as some
other mutex on your machine). Something like this works well:

Mutex m = new Mutex(true, "appname" + myScriptName, out okToRun);

je*****@san.rr.com wrote:
Mutex' are used across application boundaries. Right before running a
script, make sure you can create a mutex for it with a valid return.
When the script is complete, close the Mutex.

Something like this:

bool okToRun;
Mutex m = new Mutex(true, myScriptName, out okToRun);
if (okToRun)
{
// run script here
m.Close();
m = null;
}

-Jerry

Nov 29 '05 #4
Maybe it's enough to open the script file and lock it (FileShare.None),
and close it after the script execution ends
I have a C# app that runs script files. The app is launched using a
command line argument to indicate which script should run. Most
scripts are run indefinitely in a loop and periodically perform tasks.
When the script is launched the Title Bar of the app indicates the name
of the app and the name of the script being run.

I thought to use a GetProcessesByName to return all instances of the
app and then to check the MainWindowTitle of each process to identify
that a script is running more than one instance. This almost works.
When the app is minimized, it runs in the system tray and the
MainWindowTitle returns and empty string.

While it is not difficult to prevent multiple instances of the app from
launching this is not the desired effect. The app may kick off any
number of instances. What matters is that the same script file is not
running under more than one instance of the app.

I realize I could record state information in a database or in a file
and check it upon launching the app, but there are timeliness issues
related to this approach -- the app may be too involved doing other
tasks to take the necessary time to update the state information for at
least several moments. Therefore, this approach is not
up-to-the-second accurate.

I considered checking scripts in and out. However, an abnormal
termination of the app may not properly check in a script as available.

Any ideas for making sure that only one instance of the app is running
any given script file?

Mario T. Lanza
Clarity Information Architecture, Inc.
2005.11

Nov 30 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tom Ostberg | last post: by
1 post views Thread by Jonathan & Maricel Espejo | last post: by
2 posts views Thread by Dennis | last post: by
6 posts views Thread by Nosferatum | last post: by
4 posts views Thread by chemicalclothing | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.