Coding an application to be single instance?

J

Jeffery Tyree

I am writing an application in C#.NET that is "AlwaysOnTop" and there should
only be one instance of this program running at any given time. The
"AlwaysOnTop" piece is working just fine but I need to know how to prevent
multiple instances of this program from running. I've been away from coding
for quite a while... Back in the old day with k&r C writing a Win3.x
application, I seem to remember being able to simply state somewhere whether
the program would or would not be a multiple-instance program and away you
went. Does such an easy parameter still exist? The only information I've
been able to find involves a bit of coding where a an 'App' class has to be
derived from UserApplicationContext. I did think about just running through
the currently running processes and comparing process handles. There must
be a simple straight forward way to accomplish this, yeah?
 
N

Nicholas Paldino [.NET/C# MVP]

Jeffery,

Are you running .NET 1.1 or 2.0?

If you are running 1.1 or before, then you will have to use a mutex to
limit access. Basically, you will create a unique name for your mutex (the
assembly qualified name of the type that has the entry point to your program
will do nicely).

Then, before you call the static Run method with the Application class,
you would try and get ownership with this Mutex. If you can, then you run
the app, if you can't, you simply exit out.

In .NET 2.0, it is significantly easier. All you have to do is create a
class that derives from WindowsFormsApplicationBase in the
Microsoft.VisualBasic namespace. In the constructor, you set the
IsSingleInstance property to true, and then set the MainForm property to an
instance of your form.

Then, in your entry point, call the Run method on an instance of your
derived class, and viola, single instance semantics.

Hope this helps.
 
P

Peter Rilling

Two ways that I can see.

1) Use some form of mutex flag such as a local file that is opened for
exclusive read. Then the next instance would try to open that file and get
an error so you know some other instance is working.
2) Use the Process.GetProcessesByName and close the app if more than one
instance is returned. Have never done this but I would think it would work.
 
N

Nicholas Paldino [.NET/C# MVP]

Whatever you do, do NOT go with #2. It is incredibly inefficient, and
not accurate, either.
 
P

Peter Rilling

VB? What a shame they don't have this for C#. Such a system should be part
of the core library.

I never thought I would say it, but VB seems to have some constructs that C#
should have.


Nicholas Paldino said:
Jeffery,

Are you running .NET 1.1 or 2.0?

If you are running 1.1 or before, then you will have to use a mutex to
limit access. Basically, you will create a unique name for your mutex
(the assembly qualified name of the type that has the entry point to your
program will do nicely).

Then, before you call the static Run method with the Application class,
you would try and get ownership with this Mutex. If you can, then you run
the app, if you can't, you simply exit out.

In .NET 2.0, it is significantly easier. All you have to do is create
a class that derives from WindowsFormsApplicationBase in the
Microsoft.VisualBasic namespace. In the constructor, you set the
IsSingleInstance property to true, and then set the MainForm property to
an instance of your form.

Then, in your entry point, call the Run method on an instance of your
derived class, and viola, single instance semantics.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeffery Tyree said:
I am writing an application in C#.NET that is "AlwaysOnTop" and there
should only be one instance of this program running at any given time. The
"AlwaysOnTop" piece is working just fine but I need to know how to prevent
multiple instances of this program from running. I've been away from
coding for quite a while... Back in the old day with k&r C writing a
Win3.x application, I seem to remember being able to simply state
somewhere whether the program would or would not be a multiple-instance
program and away you went. Does such an easy parameter still exist? The
only information I've been able to find involves a bit of coding where a
an 'App' class has to be derived from UserApplicationContext. I did think
about just running through the currently running processes and comparing
process handles. There must be a simple straight forward way to
accomplish this, yeah?
 
G

Guest

Here's the details of the solution Nicholas mentioned - it's actually
extracted from one of our test conversions of a VB 2005 project and
reproduces the VB "application framework" defaults:

namespace YourRootNamespace
{
namespace My
{

internal partial class MyApplication :
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{

[global::System.Diagnostics.DebuggerStepThrough()]
public MyApplication() :
base(Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
{
this.IsSingleInstance = false;
this.EnableVisualStyles = true;
this.SaveMySettingsOnExit = true;
this.ShutdownStyle =
Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses;
}

[global::System.Diagnostics.DebuggerStepThrough()]
protected override void OnCreateMainForm()
{
this.MainForm = new global::YourRootNamespace.YourForm();
}

[STAThread]
static void Main(string[] args)
{
MyApplication MyApp = new MyApplication();
MyApp.Run(args);
}

}
}

} //end of root namespace

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
N

Nicholas Paldino [.NET/C# MVP]

I don't even see it as that. Microsoft.VisualBasic.dll is distributed
with the framework standard, and to me, an assembly is an assembly is an
assembly...

I would expect the WPF to have this baked-in though. It won't matter at
that point.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Rilling said:
VB? What a shame they don't have this for C#. Such a system should be
part of the core library.

I never thought I would say it, but VB seems to have some constructs that
C# should have.


Nicholas Paldino said:
Jeffery,

Are you running .NET 1.1 or 2.0?

If you are running 1.1 or before, then you will have to use a mutex to
limit access. Basically, you will create a unique name for your mutex
(the assembly qualified name of the type that has the entry point to your
program will do nicely).

Then, before you call the static Run method with the Application
class, you would try and get ownership with this Mutex. If you can, then
you run the app, if you can't, you simply exit out.

In .NET 2.0, it is significantly easier. All you have to do is create
a class that derives from WindowsFormsApplicationBase in the
Microsoft.VisualBasic namespace. In the constructor, you set the
IsSingleInstance property to true, and then set the MainForm property to
an instance of your form.

Then, in your entry point, call the Run method on an instance of your
derived class, and viola, single instance semantics.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeffery Tyree said:
I am writing an application in C#.NET that is "AlwaysOnTop" and there
should only be one instance of this program running at any given time.
The "AlwaysOnTop" piece is working just fine but I need to know how to
prevent multiple instances of this program from running. I've been away
from coding for quite a while... Back in the old day with k&r C writing a
Win3.x application, I seem to remember being able to simply state
somewhere whether the program would or would not be a multiple-instance
program and away you went. Does such an easy parameter still exist? The
only information I've been able to find involves a bit of coding where a
an 'App' class has to be derived from UserApplicationContext. I did
think about just running through the currently running processes and
comparing process handles. There must be a simple straight forward way
to accomplish this, yeah?
 
J

Jeffery Tyree

Thanks for all the suggestions and samples of code. In the end I opted for
the quickest and easiest way out; creating / checking for existing mutex
object.

-Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top