Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Coding an application to be single instance?

9 views
Skip to first unread message

Jeffery Tyree

unread,
Dec 2, 2005, 1:21:50 PM12/2/05
to
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?

Nicholas Paldino [.NET/C# MVP]

unread,
Dec 2, 2005, 1:33:25 PM12/2/05
to
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]
- m...@spam.guard.caspershouse.com

"Jeffery Tyree" <Jeffer...@yahoo.com> wrote in message
news:Ot2zE12...@TK2MSFTNGP15.phx.gbl...

Peter Rilling

unread,
Dec 2, 2005, 1:34:18 PM12/2/05
to
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" <Jeffer...@yahoo.com> wrote in message
news:Ot2zE12...@TK2MSFTNGP15.phx.gbl...

Nicholas Paldino [.NET/C# MVP]

unread,
Dec 2, 2005, 1:49:57 PM12/2/05
to
Whatever you do, do NOT go with #2. It is incredibly inefficient, and
not accurate, either.

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

"Peter Rilling" <pe...@nospam.rilling.net> wrote in message
news:eV0FC829...@tk2msftngp13.phx.gbl...

Peter Rilling

unread,
Dec 2, 2005, 1:52:01 PM12/2/05
to
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]" <m...@spam.guard.caspershouse.com> wrote in
message news:u0wXc729...@TK2MSFTNGP11.phx.gbl...

David Anton

unread,
Dec 2, 2005, 2:04:01 PM12/2/05
to
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

Nicholas Paldino [.NET/C# MVP]

unread,
Dec 2, 2005, 3:00:55 PM12/2/05
to
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]
- m...@spam.guard.caspershouse.com

"Peter Rilling" <pe...@nospam.rilling.net> wrote in message
news:eQB57F39...@tk2msftngp13.phx.gbl...

Jeffery Tyree

unread,
Dec 6, 2005, 2:12:54 PM12/6/05
to
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


0 new messages