473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Preventing multiple instances of windows forms app

Does anyone know how to prevent multiple applications from being
instantiated?
Nov 19 '05 #1
7 6994
Hello,

"Joe Ocampo" <jo*********@harcourt.com> schrieb:
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
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #2
I use the following Function on Sub Main()
Function PrevInstance() As Boolean
If
UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function



"Joe Ocampo" <jo*********@harcourt.com> escreveu na mensagem
news:uf**************@TK2MSFTNGP09.phx.gbl...
Does anyone know how to prevent multiple applications from being
instantiated?

Nov 19 '05 #3
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/
----------------------------------------------------------------------------
----------

"Joe Ocampo" <jo*********@harcourt.com> wrote in message
news:uf**************@TK2MSFTNGP09.phx.gbl...
Does anyone know how to prevent multiple applications from being
instantiated?

Nov 19 '05 #4
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.tThereIsAnInstanceOfThisProgramAlreadyRunn ing (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[i] != 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
Nov 19 '05 #5
Hi again,

Oops!!
Forgot I'm in the vb newsgroup, lol.

Fergus
Nov 19 '05 #6
Hello,

"Fergus Cooney" <wo****@tesco.net> schrieb:
if (aoProcList[i] != 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
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #7
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
Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Derrick | last post by:
I have a basic C# windows forms app where drag and drop of a simple class, no gui, from one tree to another (I store the class instance in the "tag" field of the tree nodes, and drag that) That is...
3
by: Boni | last post by:
Dear all, I create a big number of a class instances of some class. Sometimes this instances must be collected (all pointers are deleted, but instance is not explicitly disposed).But sometimes I...
2
by: Helen Trim | last post by:
I have an application with three forms that are msde visible and activated when needed. It uses Word to open documents and one of the forms is opened as the Word document is closed in the...
6
by: Bob Alston | last post by:
Looking for someone with experience building apps with multiple instances of forms open. I am building an app for a nonprofit organizations case workers. They provide services to the elderly. ...
6
by: Savante | last post by:
I have been writing a datalogging application. It reads in double's into a database. I want to be able to click on a row in a database (holds name of variable and also current value of variable)...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
4
by: nottarealaddress | last post by:
I'm trying to get my feet wet in VB2005 (our new standard at work after officially stopping new development in VB6 about a month ago). I'm working with a simple sql 2005 table of 50 entries, one...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.