Re: Limit C# console app to one instance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On May 25, 5:15 am, Michael Justin <michael.jus...@xxxxxxx> wrote:
j1mb0jay wrote:
http://www.codeproject.com/KB/cs/CSSIApp.aspx

"Single-Instance C# Application - for .NET 2.0"

This looks interesting, however it seems to force the developer to use
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
for the main class, so imho this is not a "lightweight" solution.

But it at least this is a nice example how .NET allows to mix libraries
from different language 'universes'.

I would recommend Mutex, which I have found here:

http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx

A nice thing is that Mutex supports "global" and "local" visibility in
terminal services environments. So two different sessions are not able
to launch the same application - this can be a real life-saver for some
apps.

Best Regards
--
Michael Justin
SCJP, SCJA
betasoft - Software for Delphi™ and for the Java™ platformhttp://www.mikejustin.com-http://www.betabeans.de

Place "using System.Threading;" at the top of the file, and include
this code in the Main:

1: // do not allow to run twice
2: bool ownsMutex; Mutex mutex = null;
3: try
4: {
5:
6: mutex = new Mutex(false, Application.ProductName,
out ownsMutex);
7: if (!ownsMutex)
8: {
9: Console.Writeline(Application.ProductName + "
is already running.");
10: return;
11: }
12: GC.KeepAlive(mutex);
13: }
14: catch
15: {
16: }
.



Relevant Pages

  • Re: iPad Thoughts
    ... technical similarity with the Mac allowed it to draw on the Mac ... They drew on the Mac developer community, ... produce the relevant kind of apps). ...
    (comp.sys.mac.advocacy)
  • Re: iPad Thoughts
    ... produce the relevant kind of apps). ... Apple appealed somehow to the upper crust of the Windows developer community, it'd still include a lot of programmers with no inclination towards UI. ...
    (comp.sys.mac.advocacy)
  • Re: Remember Paradise? Remember Inferno? Play again on iOS devices from today.
    ... With Android, my next handset will probably not be Motorola. ... Well, it's not really lock-in, any more than if I use a PC and want to switch to Mac I'm going to have to find equivalents of my PC applications for the Mac - or Linux, if that's where I'm headed. ... When it comes to third party apps, openness is up to the developer, not Apple, Google or Microsoft. ...
    (comp.sys.acorn.misc)
  • Re: About that Android fragmentation (or why the app market sucks)
    ... less likely to buy apps ... I'm saying that Android users are less likely to buy apps because of ... developer who complains about the Android app market. ... point that you can't compare good games to crappy games. ...
    (comp.sys.mac.advocacy)
  • Re: Andriod - coming on strong!
    ... downloaded 10 Billion+ iOS apps. ... Flash Professional CS5, or any text editor to build their applications ... But not from J. Random Developer. ... and download music at the same time. ...
    (comp.sys.mac.advocacy)