A Mutex holds an OS handle and implemnts IDisposable, so you need to
Dispose of the instance, the easiest to do is by means of a using block
like this....
...
using(Mutex AppMutex = new Mutex(true, "IPM_Licence_Editor", out
FirstInstance))
{
if (FirstInstance == true)
{
...
}// end of using block, AppMutex will be disposed and its handle freed.
Willy.
My code runs in the Main() function, before the main form is created,
so I would expect the I didn't need a Dispose() override (I could be
totally wrong - this is a learning exercise).
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool FirstInstance = false;
Mutex AppMutex = new Mutex(true, "IPM_Licence_Editor", out
FirstInstance);
if (FirstInstance == true)
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
}
Other variant's I've tried include maning the Mutex a static variable
and including an AppMutex.ReleaseMutex() after the Application.Run.
What actually happens is that every tmie I click on my app (after the
first instance), a new instance is created and it appears to hang at
the creation of the Mutex. I can see them listed in TaskManager. When I
close the one "running" copy, I get the AbandonedMutex exception and
all instances close.
Steve
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message Hi,
Are you releasing the mutex when you close your app? using
ReleaseMutex ?
Also, I would guess it implement IDisposable, so you better call
Dispose too.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I've been trying to do this all afternoon. The code looks Ok and I'm
getting a single instance of my app started, but I'm also getting an
"Abandoned Mutex Exception" generated when my app closes. I've had a
look on CodeProject.Com and it appears that this is probably a
problem with the .Net 2.0 Beta 2.
Maybe looking at process id's is safer after all. I'm not desperate
for red-hot performance.
Steve
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message Hi,
Use a mutex:
http://www.yoda.arachsys.com/csharp/faq/#one.application.instance
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I want to ensure that there is only ever one instance of my app
running on a single PC at any time. I understand that I can achieve
this by using a mutex and, if I can't take ownership of the mutex, I
know there is another instance of my program running.
Problem is, how do I get the "new" instance to communicate with the
"old" instance? The new one will have been started with a command
line parameter that I want to pass to the old instance for
execution. In my VB6 days, I did this using DDE (old, but safe).
What's the C# equivalent?
Can you point me at the classes I might need to work with please
(No detailed code, please - I'm trying to learn this stuff for
myself).
Thanks
Steve