473,387 Members | 1,722 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,387 software developers and data experts.

Application instance

How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks
Nov 20 '05 #1
9 1490
Option Strict On
Imports System.Threading

Module Main
Private Sub SubMain()

SingletonApp.Run(New MyForm)

End Sub
Enc Module
Public Class SingletonApp

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form)
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean

' use this to create a unique guid for your app, or Tools->Create
GUID...
'Dim g As New Guid
'g = Guid.NewGuid
'Debug.WriteLine(g.ToString)

m_Mutex = New Mutex(False, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4") '
arbitrary GUID
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks

Nov 20 '05 #2
The only sure way is to create a Mutex and try to acquire ownership of it
every time your app opens.

Luckily using a mutex from .NET is simple.

--
Klaus H. Probst, MVP
http://www.vbbox.com/
"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks

Nov 20 '05 #3
Hi,

Start your app from sub main use a mutex to see if there is another
instance running.

Public Sub main()

Dim owned As Boolean

Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned)

If owned Then

Application.Run(New Form1)

mut.ReleaseMutex()

Else

MessageBox.Show("A previous instance is already running")

End If

End Sub

Ken

------------------------

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks

Nov 20 '05 #4
* "T Cordon" <tc******@hotmail.com> scripsit:
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?


<URL:http://www.pobox.com/~skeet/csharp/faq/#one.application.instance>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
Windows forms Tips and Tricks has an example that also brings the running
instance to the foreground.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks

Nov 20 '05 #6
Having detected another instance of the application is it possible to make that instance become the active application instead of
MessageBox.Show("A previous instance is already running")

Thanks
"Ken Tucker [MVP]" wrote:
Hi,

Start your app from sub main use a mutex to see if there is another
instance running.

Public Sub main()

Dim owned As Boolean

Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned)

If owned Then

Application.Run(New Form1)

mut.ReleaseMutex()

Else

MessageBox.Show("A previous instance is already running")

End If

End Sub

Ken

------------------------

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks


Nov 20 '05 #7
Um.... I might be missing something blindling obvious here... but when I
upgraded some VB6 code the other day, it changed my code to this;

Public Sub Main()
Try
' Disallow other copies running
If GetProcessesByName(GetCurrentProcess.ProcessName). GetUpperBound(0)
= 0 Then
Application.EnableVisualStyles()
Application.Run(New frmMain)
End If
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

I take it this code neglects to let your application run if another process
with the same name also happens to be running... I suppose if your app is
called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
_______________________
The Grim Reaper

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so that
ir doesn't run multiple instances?
Thanks

Nov 20 '05 #8
Curious. What did the VB 6 code look like before the upgrade?

Yes, simply checking for the same named process is not very full proof. I
am surprised the upgrade wizard would go that route. A mutex seems to be the
safer bet.

Greg

"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cc**********@hercules.btinternet.com...
Um.... I might be missing something blindling obvious here... but when I
upgraded some VB6 code the other day, it changed my code to this;

Public Sub Main()
Try
' Disallow other copies running
If GetProcessesByName(GetCurrentProcess.ProcessName). GetUpperBound(0)
= 0 Then
Application.EnableVisualStyles()
Application.Run(New frmMain)
End If
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

I take it this code neglects to let your application run if another
process
with the same name also happens to be running... I suppose if your app is
called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
_______________________
The Grim Reaper

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so
that
ir doesn't run multiple instances?
Thanks


Nov 20 '05 #9
Oh yes, I agree the mutex thing looks a whole lot more solid!! The old VB6
code (I've found it in most of my old projects after a brief search - dunno
where I got it from) was something like this;

Sub Form_Load()
If App.PrevInstance Then
Msgbox "<app name> is already running!", vbOKOnly + vbInformation,
"Stupid User Alert"
Unload Me
End If
End Sub

Seems oh-so-simple looking back... lol.
____________________________
The Grim Reaper

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
Curious. What did the VB 6 code look like before the upgrade?

Yes, simply checking for the same named process is not very full proof. I
am surprised the upgrade wizard would go that route. A mutex seems to be the safer bet.

Greg

"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cc**********@hercules.btinternet.com...
Um.... I might be missing something blindling obvious here... but when I
upgraded some VB6 code the other day, it changed my code to this;

Public Sub Main()
Try
' Disallow other copies running
If GetProcessesByName(GetCurrentProcess.ProcessName). GetUpperBound(0) = 0 Then
Application.EnableVisualStyles()
Application.Run(New frmMain)
End If
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

I take it this code neglects to let your application run if another
process
with the same name also happens to be running... I suppose if your app is called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
_______________________
The Grim Reaper

"T Cordon" <tc******@hotmail.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
How to determine if another instance of the application is running so
that
ir doesn't run multiple instances?
Thanks



Nov 20 '05 #10

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

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
4
by: Chuck | last post by:
Hello everybody, I need to abort execution during start up, while the constructor called by Application.Run is executing. If the database fails to connect during my application's startup I...
8
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance;...
6
by: Tony Fonager | last post by:
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application. (I have simplified my code, to make my project easier to...
3
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to...
2
by: Mesan | last post by:
Hello everyone, Thanks to many useful posts in this newsgroup and others, I've been able to come very close to something I've been wanting to do for a very long time. I've figured out how to...
2
by: pamela fluente | last post by:
I have an application running. A file type is registered with this application. When the user click on a file of such type a new instance of the application is loaded with command line (file name)....
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
3
by: Joseph Geretz | last post by:
I'm implementing a web application whose purpose in life is to act as a data conduit. Data is posted to my Web app in XML format, my application examines the data and forwards it onward by posting...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.