Preventing multiple instances of windows forms app

H

Herfried K. Wagner [MVP]

Hello,

Joe Ocampo said:
Does anyone know how to prevent multiple applications from being
instantiated?

\\\
using System.Threading;
..
..
..
Mutex m = new Mutex(false,"blablablabla92389321osdk");
if (m.WaitOne(10, false))
{
Application.Run(new Form1());
m.ReleaseMutex();
}
else // Beenden.
MessageBox.Show("Anwendung wird bereits ausgeführt!");
///

HTH,
Herfried K. Wagner
 
M

Marcos Ribeiro

I use the following Function on Sub Main()
Function PrevInstance() As Boolean
If
UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
 
K

Kumar Gaurav Khanna [.NET MVP]

Hi!

Use the GetProcesses static method of the System.Diagnostics class to get an
array of Process objects, for each of the running processes and determine if
the application in question is running or not.

Regards.
Kumar Gaurav Khanna

--
----------------------------------------------------------------------------
----------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/
 
F

Fergus Cooney

Hi Joe,

My version of this method is a bit longer-winded because I put it in an
application class where I can have all application-wide stuff together for
ease of re-use. I like my "if" statements to read as English, too.

public class appFoo
{
static void Main (string[] asArgs)
{
if (clsApp.tThereIsAnInstanceOfThisProgramAlreadyRunning (true)) //
true = Activate the prior instance.
{
Application.Exit(); // Exit quietly, leaving control with the
original.
return;
}
: : :
Application.Run (new frmFoo());
}
}

public class clsApp
{
protected clsApp() {}// Inaccessible constructor - No object instances
allowed.

/// <summary> Returns True if so. </summary>
public static bool tThereIsAnInstanceOfThisProgramAlreadyRunning()
{ return tThereIsAnInstanceOfThisProgramAlreadyRunning (false); }


public static bool tThereIsAnInstanceOfThisProgramAlreadyRunning (bool
tToActivateThePreviousInstance)
{
Process oThisProcess;
Process[] aoProcList;

oThisProcess = Process.GetCurrentProcess();
aoProcList = Process.GetProcessesByName
(oThisProcess.ProcessName); // At least 1.

if (aoProcList.Length == 1)
return false; // There's just the current process.

if (tToActivateThePreviousInstance)
for (uint i = 0; i < aoProcList.Length; i++)
if (aoProcList != oThisProcess)
{
// Activate the previous instance.
//!! AppActivate() is a Vb function.
// Q. How is it called in C# ?
// A. Add a reference to VBA. Import it. Call it.
// Q. Is there a better way ?
// A. Calling Herfried ?? Come in Herfried ??
break;
}

return true;
}
}

As you can see I still haven't found a neat C# way of activating the
previous instance. Can anyone help on this. I don't want to use VB
facilities, nor the WinApi, unless I have to.

Have fun,
Fergus
 
H

Herfried K. Wagner [MVP]

Hello,

Fergus Cooney said:
if (aoProcList != oThisProcess)
{
// Activate the previous instance.
//!! AppActivate() is a Vb function.
// Q. How is it called in C# ?
// A. Add a reference to VBA. Import it. Call it.
// Q. Is there a better way ?
// A. Calling Herfried ?? Come in Herfried ??


I would add a reference to Microsoft.VisualBasic.dll and use
AppActivate.
previous instance. Can anyone help on this. I don't want to use VB
facilities, nor the WinApi, unless I have to.

Why don't use the VB .NET functions? They are part of the "Framework".

HTH,
Herfried K. Wagner
 
F

Fergus Cooney

Previously in this thread...
Fergus: // Activate the previous instance.
//!! AppActivate() is a Vb function.
// Q. How is it called in C# ?
// A. Add a reference to VBA. Import it. Call it.
// Q. Is there a better way ?
// A. Calling Herfried ?? Come in Herfried ??
Can anyone help on this. I don't want to use VB facilities, nor the WinApi,
unless I have to.
Herfried:
I would add a reference to Microsoft.VisualBasic.dll and use AppActivate.
Why don't use the VB .NET functions? They are part of the "Framework".


Hi Herfried,

The reasoning is more emotional than logical.

I love .NET for many reasons. One of these is that it is a new design, a
fresh start, a new starting point.

I don't mind mixing languages at the level of assemblies, but within my
C# code I'd prefer to use just C# facilities. Even within my VB.NET code I
want to get away from old-style VB coding, like Left, Mid, etc. To me, these
things, and AppActivate, seem to be included in the sense of
backward-compatibility. While its true that Microsoft.VisualBasic is part of
the Framework, it feels, to me, that it isn't as "valid", somehow, as the
rest of the Framework. I'd like there to be something in the Process class,
but, boo hoo, I haven't seen anything.

Like I said, emotional logic. AppActivate will do the job. It's part of
the Framework. It's just that it feels like I'm stepping "outside" to do the
job.

I wonder if this makes sense to you :)

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top