471,315 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Coding an application to be single instance?

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?

Dec 2 '05 #1
7 1833
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]
- mv*@spam.guard.caspershouse.com

"Jeffery Tyree" <Je***********@yahoo.com> wrote in message
news:Ot*************@TK2MSFTNGP15.phx.gbl...
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?

Dec 2 '05 #2
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.

"Jeffery Tyree" <Je***********@yahoo.com> wrote in message
news:Ot*************@TK2MSFTNGP15.phx.gbl...
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?

Dec 2 '05 #3
Whatever you do, do NOT go with #2. It is incredibly inefficient, and
not accurate, either.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
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.

"Jeffery Tyree" <Je***********@yahoo.com> wrote in message
news:Ot*************@TK2MSFTNGP15.phx.gbl...
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?


Dec 2 '05 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u0**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jeffery Tyree" <Je***********@yahoo.com> wrote in message
news:Ot*************@TK2MSFTNGP15.phx.gbl...
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?


Dec 2 '05 #5
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.WindowsF ormsApplicationBase
{

[global::System.Diagnostics.DebuggerStepThrough()]
public MyApplication() :
base(Microsoft.VisualBasic.ApplicationServices.Aut henticationMode.Windows)
{
this.IsSingleInstance = false;
this.EnableVisualStyles = true;
this.SaveMySettingsOnExit = true;
this.ShutdownStyle =
Microsoft.VisualBasic.ApplicationServices.Shutdown Mode.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

"Jeffery Tyree" wrote:
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?

Dec 2 '05 #6
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]
- mv*@spam.guard.caspershouse.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eQ**************@tk2msftngp13.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:u0**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jeffery Tyree" <Je***********@yahoo.com> wrote in message
news:Ot*************@TK2MSFTNGP15.phx.gbl...
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?



Dec 2 '05 #7
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
Dec 6 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by M.Siler | last post: by
19 posts views Thread by auratius | last post: by
5 posts views Thread by Frank Moyles | last post: by
3 posts views Thread by =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post: by
3 posts views Thread by Joseph Geretz | 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.