473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Preventing an app from being launched more than once

Hi,

I'm using the following example for this:
http://www.yoda.arachsys.com/csharp/...ation.instance

which works perfectly on Vista, but not on XP...

Doesn't throw any errors, but multiple launches of the app are not
prevented.

Is there another solution?

Any assistance gratefully received.

--
http://www.markrae.net

Jun 19 '07 #1
22 1694
Mark,

This previous thread indicates how:

http://groups.google.com/group/micro...8f00c1a?hl=en&

Basically, you are going to use the WindowsFormsApplicationBase class in
the Microsoft.VisualBasic namespace.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Hi,

I'm using the following example for this:
http://www.yoda.arachsys.com/csharp/...ation.instance

which works perfectly on Vista, but not on XP...

Doesn't throw any errors, but multiple launches of the app are not
prevented.

Is there another solution?

Any assistance gratefully received.

--
http://www.markrae.net

Jun 19 '07 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
This previous thread indicates how:

http://groups.google.com/group/micro...8f00c1a?hl=en&

Basically, you are going to use the WindowsFormsApplicationBase class
in the Microsoft.VisualBasic namespace.
Ouch! Use the Microsoft.VisualBasic namespace in a C# app...?

Hmm - OK, I guess I'll have to stop being so anal about that!

Curious that Jon's code works perfectly on Vista, though...
--
http://www.markrae.net

Jun 19 '07 #3
Mark Rae wrote:
Hi,

I'm using the following example for this:
http://www.yoda.arachsys.com/csharp/...ation.instance

which works perfectly on Vista, but not on XP...

Doesn't throw any errors, but multiple launches of the app are not
prevented.

Is there another solution?

Any assistance gratefully received.
Hi,

Did you see the comment about it being a local mutex? Are you testing
multiple launches of the application within a single user session, or
across user sessions (e.g. terminal services or fast user switching)?

If the latter, you'll need to make the mutex global. If the former, then
it's strange that it doesn't work... Perhaps it's a permissions issue.

--
Tom Spink
University of Edinburgh
Jun 19 '07 #4
"Tom Spink" <ts****@gmail.comwrote in message
news:OV**************@TK2MSFTNGP05.phx.gbl...
Did you see the comment about it being a local mutex?
Yes.
Are you testing multiple launches of the application within a single user
session,
Yes.
or across user sessions (e.g. terminal services or fast user switching)?
No.
If the latter, you'll need to make the mutex global.
Indeed, but it still doesn't work on XP either as Local or Global.
If the former, then it's strange that it doesn't work...
I agree. Works perfectly on Vista, though...
Perhaps it's a permissions issue.
I'm logged on to XP as an administrator.
--
http://www.markrae.net

Jun 19 '07 #5
Mark Rae wrote:
"Tom Spink" <ts****@gmail.comwrote in message
news:OV**************@TK2MSFTNGP05.phx.gbl...
>Did you see the comment about it being a local mutex?

Yes.
>Are you testing multiple launches of the application within a single user
session,

Yes.
>or across user sessions (e.g. terminal services or fast user switching)?

No.
>If the latter, you'll need to make the mutex global.

Indeed, but it still doesn't work on XP either as Local or Global.
>If the former, then it's strange that it doesn't work...

I agree. Works perfectly on Vista, though...
>Perhaps it's a permissions issue.

I'm logged on to XP as an administrator.

Hi Mark,

Would you be able to share the code you're using, please?

--
Tom Spink
University of Edinburgh
Jun 19 '07 #6
Have not tested this on Vista, but this should work under fx2.0:

internal bool CanStart()
{
Semaphore s = new Semaphore(1, 1,
Assembly.GetExecutingAssembly().GetName().FullName );
if (s.WaitOne(1000, false))
{
// Got ownership.
this.FormClosed += new FormClosedEventHandler(
delegate(object sender, FormClosedEventArgs e)
{
s.Release(); // Capture the local variable in this
closure and release when form closed.
MessageBox.Show("Sem Released.");
});
return true;
}
return false; // Could not get ownership.
}

private void Form1_Load(object sender, EventArgs e)
{
if (!CanStart())
{
MessageBox.Show("Already Started");
this.Close();
return;
}
...
}

--
William Stacey [C# MVP]
PowerLocker, PowerPad
www.powerlocker.com

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
| Hi,
|
| I'm using the following example for this:
| http://www.yoda.arachsys.com/csharp/...ation.instance
|
| which works perfectly on Vista, but not on XP...
|
| Doesn't throw any errors, but multiple launches of the app are not
| prevented.
|
| Is there another solution?
|
| Any assistance gratefully received.
|
| --
| http://www.markrae.net
|
Jun 19 '07 #7
"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Would you be able to share the code you're using, please?
http://www.yoda.arachsys.com/csharp/...ation.instance
--
http://www.markrae.net

Jun 19 '07 #8
Mark,

In the end, it is just IL, and you know it will be distributed as part
of the framework. The best code you are going to write is the code you
never have to write or maintain.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP05.phx.gbl...
> This previous thread indicates how:

http://groups.google.com/group/micro...8f00c1a?hl=en&

Basically, you are going to use the WindowsFormsApplicationBase class
in the Microsoft.VisualBasic namespace.

Ouch! Use the Microsoft.VisualBasic namespace in a C# app...?

Hmm - OK, I guess I'll have to stop being so anal about that!

Curious that Jon's code works perfectly on Vista, though...
--
http://www.markrae.net
Jun 20 '07 #9
On Jun 20, 12:41 am, "Mark Rae" <m...@markNOSPAMrae.netwrote:
Would you be able to share the code you're using, please?

http://www.yoda.arachsys.com/csharp/...ation.instance
Well, that's just a snippet - could you show the whole code, in the
form of a short but complete program?

Jon

Jun 20 '07 #10
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Hi,

I'm using the following example for this:
http://www.yoda.arachsys.com/csharp/...ation.instance

which works perfectly on Vista, but not on XP...

Doesn't throw any errors, but multiple launches of the app are not
prevented.

Is there another solution?

Any assistance gratefully received.

--
http://www.markrae.net

Make sure your Mutex doesn't get collected before this program instance
terminates, one way to achieve this is by applying the using idiom like
this:.

....
bool firstInstance;
using (Mutex AppMutex = new Mutex(true, new
Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString(), out firstInstance))
{
if (firstInstance== true)
{
// run your program code from here
}
} // end using scope, mutex released here.
....

where: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is just some valid uuid, but of
course you are free to use anything else to uniquely identify the program.

Willy.

Jun 20 '07 #11
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
On Jun 20, 12:41 am, "Mark Rae" <m...@markNOSPAMrae.netwrote:
Would you be able to share the code you're using, please?

http://www.yoda.arachsys.com/csharp/...ation.instance

Well, that's just a snippet - could you show the whole code, in the
form of a short but complete program?
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

bool blnFirstInstance;
Mutex objMutex = new Mutex(false, "Local\\" + "abcdefg", out
blnFirstInstance);
if (!blnFirstInstance)
{
MessageBox.Show("App is already running", "Single Use Only",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}

// rest of Main() code
}

Works perfectly on 64-bit Vista Business, doesn't work on 32-bit
WinXPPro+SP2 - haven't tried on any other version of Windows...
--
http://www.markrae.net

Jun 20 '07 #12
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:Os**************@TK2MSFTNGP03.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
>On Jun 20, 12:41 am, "Mark Rae" <m...@markNOSPAMrae.netwrote:
>Would you be able to share the code you're using, please?

http://www.yoda.arachsys.com/csharp/...ation.instance

Well, that's just a snippet - could you show the whole code, in the
form of a short but complete program?

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

bool blnFirstInstance;
Mutex objMutex = new Mutex(false, "Local\\" + "abcdefg", out
blnFirstInstance);
if (!blnFirstInstance)
{
MessageBox.Show("App is already running", "Single Use Only",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}

// rest of Main() code
}

Works perfectly on 64-bit Vista Business, doesn't work on 32-bit
WinXPPro+SP2 - haven't tried on any other version of Windows...
--
http://www.markrae.net


Your Mutex instance will get collected whenever the GC comes along for the
first time. Now, probably the GC did not run yet in the first instance
before you started the second instance on 64-bit windows, anyway you need to
protect the mutex instance from premature collection.
My previous reply shows you how you one possibility to prevent this
premature collection.

Willy.

Jun 20 '07 #13
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Os**************@TK2MSFTNGP04.phx.gbl...
Your Mutex instance will get collected whenever the GC comes along for the
first time. Now, probably the GC did not run yet in the first instance
before you started the second instance on 64-bit windows, anyway you need
to protect the mutex instance from premature collection.
I understand what you're saying. However, on 64-bit Vista, this still works
even if I wait a whole hour before trying to run the second instance - maybe
something different happens with Mutexes on Vista and/or 64-bit Windows...?
My previous reply shows you how you one possibility to prevent this
premature collection.
Thanks - I'm looking at that now...
--
http://www.markrae.net

Jun 20 '07 #14
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
Make sure your Mutex doesn't get collected before this program instance
terminates, one way to achieve this is by applying the using idiom like
this:.

...
bool firstInstance;
using (Mutex AppMutex = new Mutex(true, new
Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString(), out
firstInstance))
{
if (firstInstance== true)
{
// run your program code from here
}
} // end using scope, mutex released here.
...

where: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is just some valid uuid, but
of course you are free to use anything else to uniquely identify the
program.

Willy.
Now this is very weird!

I tried your above code, and it worked perfectly on 64-bit Vista but not on
32-bit XP.

However, the version of XP I was testing it on was running in a Virtual PC
2007 virtual machine, so I wondered if that might be the cause, though i
couldn't imagine why...

Therefore, I tried it on XP running on a "real" machine, and it worked
perfectly!

Obviously, I'm happy that it works but I'm not particularly impressed... I
use VPC 2007 all the time for testing my apps, so I wonder what else might
behave differently in this scenario...?

Unfortunately, I don't know enough about mutexes and how they're generated,
but do you have any idea why a virtual machine might cause a mutex to behave
differently from a real machine...?

I'm about to try it on Vista running in a virtual machine, and will let you
know what happens...

I'll also post in the VPC newsgroup to see if anyone has come across this
before...
--
http://www.markrae.net

Jun 20 '07 #15
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Os**************@TK2MSFTNGP04.phx.gbl...
>Your Mutex instance will get collected whenever the GC comes along for
the first time. Now, probably the GC did not run yet in the first
instance before you started the second instance on 64-bit windows, anyway
you need to protect the mutex instance from premature collection.

I understand what you're saying. However, on 64-bit Vista, this still
works even if I wait a whole hour before trying to run the second
instance - maybe something different happens with Mutexes on Vista and/or
64-bit Windows...?
Waiting is not enough, you should allocate enough objects for the GC to kick
in (or you could force a GC collect).
Don't forget the the GC threshold for 64-bit .NET applications is quite a
bit larger than for 32-bit processes, so it can take a while before the GC
runs.
Willy.

Jun 20 '07 #16
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:Os**************@TK2MSFTNGP05.phx.gbl...
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
>Make sure your Mutex doesn't get collected before this program instance
terminates, one way to achieve this is by applying the using idiom like
this:.

...
bool firstInstance;
using (Mutex AppMutex = new Mutex(true, new
Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString(), out
firstInstance))
{
if (firstInstance== true)
{
// run your program code from here
}
} // end using scope, mutex released here.
...

where: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is just some valid uuid, but
of course you are free to use anything else to uniquely identify the
program.

Willy.

Now this is very weird!

I tried your above code, and it worked perfectly on 64-bit Vista but not
on 32-bit XP.

However, the version of XP I was testing it on was running in a Virtual PC
2007 virtual machine, so I wondered if that might be the cause, though i
couldn't imagine why...

Therefore, I tried it on XP running on a "real" machine, and it worked
perfectly!

Obviously, I'm happy that it works but I'm not particularly impressed... I
use VPC 2007 all the time for testing my apps, so I wonder what else might
behave differently in this scenario...?

Unfortunately, I don't know enough about mutexes and how they're
generated, but do you have any idea why a virtual machine might cause a
mutex to behave differently from a real machine...?

I'm about to try it on Vista running in a virtual machine, and will let
you know what happens...

I'll also post in the VPC newsgroup to see if anyone has come across this
before...

Both should behave the same, unless you are running in different logon
session, in which case you can try using a global named mutex. To do this
you should start the name with "Global\".

Willy.

Jun 20 '07 #17
Have you tried my code above?

--
William Stacey [C# MVP]
PowerLocker, PowerPad
www.powerlocker.com

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:Os**************@TK2MSFTNGP05.phx.gbl...
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:eC**************@TK2MSFTNGP04.phx.gbl...
|
| Make sure your Mutex doesn't get collected before this program instance
| terminates, one way to achieve this is by applying the using idiom like
| this:.
| >
| ...
| bool firstInstance;
| using (Mutex AppMutex = new Mutex(true, new
| Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString(), out
| firstInstance))
| {
| if (firstInstance== true)
| {
| // run your program code from here
| }
| } // end using scope, mutex released here.
| ...
| >
| where: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is just some valid uuid, but
| of course you are free to use anything else to uniquely identify the
| program.
| >
| Willy.
|
| Now this is very weird!
|
| I tried your above code, and it worked perfectly on 64-bit Vista but not
on
| 32-bit XP.
|
| However, the version of XP I was testing it on was running in a Virtual PC
| 2007 virtual machine, so I wondered if that might be the cause, though i
| couldn't imagine why...
|
| Therefore, I tried it on XP running on a "real" machine, and it worked
| perfectly!
|
| Obviously, I'm happy that it works but I'm not particularly impressed... I
| use VPC 2007 all the time for testing my apps, so I wonder what else might
| behave differently in this scenario...?
|
| Unfortunately, I don't know enough about mutexes and how they're
generated,
| but do you have any idea why a virtual machine might cause a mutex to
behave
| differently from a real machine...?
|
| I'm about to try it on Vista running in a virtual machine, and will let
you
| know what happens...
|
| I'll also post in the VPC newsgroup to see if anyone has come across this
| before...
|
|
| --
| http://www.markrae.net
|
Jun 20 '07 #18
You don't need to deal with any of that GC stuff using example I posted,
because the sem is released cleanly on form closed event. You could do same
kind of thing with a mutex.

--
William Stacey [C# MVP]
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:Os**************@TK2MSFTNGP04.phx.gbl...
|
| Your Mutex instance will get collected whenever the GC comes along for
the
| first time. Now, probably the GC did not run yet in the first instance
| before you started the second instance on 64-bit windows, anyway you
need
| to protect the mutex instance from premature collection.
|
| I understand what you're saying. However, on 64-bit Vista, this still
works
| even if I wait a whole hour before trying to run the second instance -
maybe
| something different happens with Mutexes on Vista and/or 64-bit
Windows...?
|
| My previous reply shows you how you one possibility to prevent this
| premature collection.
|
| Thanks - I'm looking at that now...
|
|
| --
| http://www.markrae.net
|
Jun 20 '07 #19
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uS**************@TK2MSFTNGP03.phx.gbl...
Both should behave the same,
I agree!
unless you are running in different logon session,
I'm not.
in which case you can try using a global named mutex.
I've tried both Local and Global, and also changing the first argument from
false to true...

However, I can further confirm that your code most certainly *does* work on
Vista installed in a virtual machuine...
--
http://www.markrae.net

Jun 20 '07 #20
"William Stacey [C# MVP]" <wi************@gmail.comwrote in message
news:%2*****************@TK2MSFTNGP02.phx.gbl...
Have you tried my code above?
Not yet, but I certainly will...
--
http://www.markrae.net

Jun 20 '07 #21
Mark Rae <ma**@markNOSPAMrae.netwrote:
Well, that's just a snippet - could you show the whole code, in the
form of a short but complete program?

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

bool blnFirstInstance;
Mutex objMutex = new Mutex(false, "Local\\" + "abcdefg", out
blnFirstInstance);
if (!blnFirstInstance)
{
MessageBox.Show("App is already running", "Single Use Only",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}

// rest of Main() code
}

Works perfectly on 64-bit Vista Business, doesn't work on 32-bit
WinXPPro+SP2 - haven't tried on any other version of Windows...
As others have said, the "using" statement is a good way to go. Another
alternative is to use GC.KeepAlive (as suggested on the web page, in
fact). It's very odd if that fails, even in a VPC.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 20 '07 #22
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
As others have said, the "using" statement is a good way to go.
Still doesn't help in XP in a virtual machine...
Another alternative is to use GC.KeepAlive (as suggested on the web page,
in
fact).
OK - I'll have a try.

It's very odd if that fails, even in a VPC.

I couldn't agree more! But it does, and not on just one VM...
--
http://www.markrae.net

Jun 20 '07 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it...
2
by: Steveo | last post by:
Hi I have a form containing some text boxes and a button. I'm trying to work out how to make it so that if certain textboxes contain no data then the button is not enabled (however still...
2
by: Bob A | last post by:
I do a weekly import of data from two text files, using two schema files. I have automated the import process that gets the data from the two files using a macro, and everything works great. ...
0
by: Erik Herje via .NET 247 | last post by:
Hi, I have a Windows Service written in C# whos purpose is to sniff a folder to look for changes in the folder. If a file is created or changed, it should get its name and start an object the...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
3
by: Bill | last post by:
I have a v2.0 site that has a form with a GO button which runs another application and then returns some data which is then processed and then I issue a response.redirect to another form to display...
12
by: Mark Rae | last post by:
Hi, See the previous thread Request.Form abuse in this newsgroup... I'm looking for a simple and efficient way to prevent people hijacking the <formtags on my websites and using them to send...
3
by: Bill Davidson | last post by:
All: I have a problem in which a worker thread in my (.dll) assembly isn't allowing the main (.exe) assembly from terminating. Here's the scenario: 1) App.Exe is launched. 2) App.Exe calls...
1
by: Ruben | last post by:
I created a simple form that includes a hyperlink field which when clicked, launches any given pdf file saved in a multi-user network. The problem is: It ONLY seems to work well when launched...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.