Re: Limit C# console app to one instance
- From: harborsparrow <pgpalmer@xxxxxxxxx>
- Date: Mon, 26 May 2008 19:14:37 -0700 (PDT)
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: }
.
- Follow-Ups:
- Re: Limit C# console app to one instance
- From: Jon Skeet [C# MVP]
- Re: Limit C# console app to one instance
- References:
- Re: Limit C# console app to one instance
- From: Michael Justin
- Re: Limit C# console app to one instance
- Prev by Date: C#.Net web form calling a Button from javascript
- Next by Date: Re: Microsoft Word an c#
- Previous by thread: Re: Limit C# console app to one instance
- Next by thread: Re: Limit C# console app to one instance
- Index(es):
Relevant Pages
|