By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

How to stop running multiple instances of my App in C#?

P: n/a
How to stop running multiple instances of my App in C#?

Thanx
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
PalB <pa*******@hotmail.com> wrote:
How to stop running multiple instances of my App in C#?


The easiest ways are to either acquire a named mutex, or try to open a
socket on a port which nothing but your app would use. Of course, both
are problematic in terms of namespace - creating a name no-one else is
likely to use for a mutex is easier than working out a port number
which no-one else is likely to use though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
A

"PalB" <pa*******@hotmail.com> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
How to stop running multiple instances of my App in C#?


I check to see if the program is already running in Main before the call to
the actual object to run it.

Of course this assumes that your program name is unique, therefore if you
have another application with the same Name, it won't run at all...

static void Main(){
if( System.Diagnostics.Process.GetProcessesByName(
"ProgramNameHere" ).Length > 1 ){
return;
}
}
Nov 13 '05 #3

P: n/a
Thanx a lot... i will chose the mutex part :)

Thanx again,
PalB

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
PalB <pa*******@hotmail.com> wrote:
How to stop running multiple instances of my App in C#?


The easiest ways are to either acquire a named mutex, or try to open a
socket on a port which nothing but your app would use. Of course, both
are problematic in terms of namespace - creating a name no-one else is
likely to use for a mutex is easier than working out a port number
which no-one else is likely to use though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #4

P: n/a
PalB <pa*******@hotmail.com> wrote:
Hi i am writing code like this.....is it ok?

static void Main()
{
bool isNew = false;
Mutex mtx = new Mutex( true, "MyApp_Mutex", out isNew );
if( !isNew )
{
MessageBox.Show( "MyApp is already running." );
return;
}
}


You don't need to specify the initial value for isNew - out parameters
don't need to be definitely assigned beforehand.

Other than that, it looks okay to me - have you tried it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.