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
using
System.Diagnostics;public static bool IsOneInstance()
{
Process pcur = Process.GetCurrentProcess();
Process[] ps = Process.GetProcesses();
foreach( Process p in ps )
{
if ( pcur.Id != p.Id )
if ( pcur.ProcessName == p.ProcessName )
return false;
}
return true;
}
"Jérôme Bonnet" <jbo...@topsys.fr> wrote in message news:4369f154$0$17217$8fcf...@news.wanadoo.fr...
Would it be possible for two of your processes to start almost
simultaneously, detect each other, return false from this method - and
therefore both terminate?
"Jérôme Bonnet" <jbo...@topsys.fr> wrote in message news:4369f154$0$17217$8fcf...@news.wanadoo.fr...
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in message news:e5niRtH4...@TK2MSFTNGP10.phx.gbl...
This is why I want to go with a Mutex, as it should take care of this
situation. In reality, it's unlikely that a user could start two copies that
fast - they'll typically be double clicking on files in Explorer. I just
never imagined it would get so complicated - I did this in a couple of lines
of ode in VB6.
Steve
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
"Steve Barnett" <non...@nodomain.com> wrote in message
news:%23AMlOKG...@TK2MSFTNGP12.phx.gbl...
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 news:%23stWg6H...@TK2MSFTNGP09.phx.gbl...
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
"Steve Barnett" <non...@nodomain.com> wrote in message
news:%23F9GcQI...@TK2MSFTNGP09.phx.gbl...
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 news:%23r52LdI...@TK2MSFTNGP15.phx.gbl...
...
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.
"Steve Barnett" <non...@nodomain.com> wrote in message
news:OCtFznI...@tk2msftngp13.phx.gbl...
static void Main()
{
bool FirstInstance = false;
using (Mutex AppMutex = new Mutex(true, "IPM_Licence_Editor", out
FirstInstance))
{
if (FirstInstance == true)
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
} // Mutex will be disposed of here
}
Which should work, I believe. However, I still have the problem that
multiple apps get started and sit there waiting for the first version to
end. When it does, all of the ones queued up fail with the Abandoned Mutex
Exception.
This isn't critical to my app right now, so I guess I'd better come round it
again when I've moved on from the Beta2 libraries.
Thanks, everyone, for the help.
Steve
"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote in message
news:OLJ06AJ4...@TK2MSFTNGP15.phx.gbl...
You have to pass false to the first parameter ( it's correctly in the link
I gave you)
from msdn:
If name is not a null reference (Nothing in Visual Basic) and initiallyOwned
is true, then the application must ensure that a mutex that has the same
name and is owned by the calling thread does not already exist. If the mutex
is being used for cross-process communication, you should set initiallyOwned
to false, or use the Mutex(Boolean, String, Boolean) constructor. Otherwise,
it will be difficult to determine which process has initial ownership.
If you pass true the mutex will wait until it adquire it. This is not what
you want !
You have no use for the mutex, you only needs to know if the mutex exist
(indicating another instance) or not.
new Mutex(false, "Local\\"+someUniqueName, out firstInstance);
is the correct set of parameters.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Steve Barnett" <non...@nodomain.com> wrote in message
news:OX6NnbJ4...@TK2MSFTNGP15.phx.gbl...
Well, that's the easy bit over, now I need to make my new instance talk to
the old instance.
Oh joy!
Thanks for all the help.
Steve
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:echNKjK4...@TK2MSFTNGP15.phx.gbl...
Willy.
It wasn't meant as a criticism of you. I do appreciate the help you and the
others offered.
Steve
"Willy Denoyette [MVP]" <willy.d...@telenet.be> wrote in message
news:%239PmztS...@TK2MSFTNGP10.phx.gbl...
If App.PrevInstance Then End
I now know it ain't gonna be as easy as the hipe promised. Still, if it was
that easy, everyone would be doing it.
Steve
"Maruthi" <Mar...@discussions.microsoft.com> wrote in message
news:5DFED991-6BCB-498F...@microsoft.com...