473,473 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how do I ensure only one instance of an application can execute concurrently

Could someone please tell me how I ensure only one instance of an
application can execute concurrently?
Jul 21 '05 #1
3 1461
Rob
using System.Runtime.InteropServices;

#region Check to see if it's already runnin
// Get this process name
string proc = Process.GetCurrentProcess().ProcessName

// Get all the processes with the same name
Process[] procs = Process.GetProcessesByName(proc)

// If there is more than 1 including this one then return
if(procs.Length > 1

// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us
// Get our process
Process p = Process.GetCurrentProcess()
int n = 0; // Assume the other process is at index 0
// If this process id is OUR process ID..
if(procs[0].Id == p.Id

// then the other process is at index 1
n = 1

// Get the window handle
IntPtr hWnd = procs[n].MainWindowHandle
// If iconic, we need to restore the window
if(IsIconic(hWnd)

ShowWindowAsync(hWnd, SW_RESTORE)
// Bring it to the foreground
SetForegroundWindow(hWnd)
// Exit this process
return

#endregion
Jul 21 '05 #2
Chris Anderson <an******@leavenworth.army.mil> wrote:
Could someone please tell me how I ensure only one instance of an
application can execute concurrently?


The best way is to use a global mutex. If you search for
mutex application one instance
on groups.google.com in relevant groups, you'll get a lot of hits.

Using the process name is more dodgy, IMO, because there may be other
processes with the same name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Try this, from MSDN (not tested but looks fine)

' Visual Basic .NET
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
Jul 21 '05 #4

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

Similar topics

0
by: Walter G | last post by:
I have a VB.Net application that can be executed with parameters. If parameters are received, the I execute a function that will open a form within the application. On top of that, I'd like to...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: Zahni | last post by:
Win 2003 Enterprise, 14 GB RAM, /3GB and /PAE DB2_AWE=1,330000,40000 Simple test: wrote two batches: :10 db2 activate database ga db2 deactivate database ga goto 10
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
6
by: Xenio | last post by:
Hi, I'd like to make sure that only one instance of my app is running per User Session. In addition this has to work with user rights and in a Windows 2k/2k3 and Citrix Terminalserver...
4
by: Chris Anderson | last post by:
Could someone please tell me how I ensure only one instance of an application can execute concurrently?
1
by: Dhruba Bandopadhyay | last post by:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm....
4
by: Timothy Grant | last post by:
On Fri, Aug 29, 2008 at 6:51 AM, Heston James <heston_james@live.co.ukwrote: Why not look at the process list, and if you find two entries kill the one that is just starting? -- Stand Fast,...
1
by: JDHCoding | last post by:
I have a macro that is pulling data from a linked table, creating several summary tables, and then exporting the results to Excel. I’d like to make this available to other users but I’m concerned...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.