473,503 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Open two instances of a windows application?

While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina
Nov 21 '05 #1
22 1910
Hi Nina,

Each instance runs in its own address space, so, no, the global variables
will not interfere with each other.

HTH,

Bernie Yaeger

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #2
Hi Nina,

Each instance runs in its own address space, so, no, the global variables
will not interfere with each other.

HTH,

Bernie Yaeger

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #3

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know programmatically if the application is already opened when user tries to open the second instance? Is there a way to prohibit user to launch more than one application at one time?


I am also interested in knowing if my application is already running when it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already running
instance of my app instead of spawning a second copy of my app?
Nov 21 '05 #4

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know programmatically if the application is already opened when user tries to open the second instance? Is there a way to prohibit user to launch more than one application at one time?


I am also interested in knowing if my application is already running when it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already running
instance of my app instead of spawning a second copy of my app?
Nov 21 '05 #5
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance
to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult.
I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:

' code to start app...
Try
SingletonApp.Run(New MyMainForm, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4")
'arbitrary GUID
Catch ex As SingletonException
MsgBox("Can not start because a previous instance of this application is
already running!")
End Try

' code inside singletonapp.vb file
Option Strict On

Imports System.Threading

Public Class SingletonApp

Private Shared _guid As String

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _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() ' think this may not be necessary???
m_Mutex.Close()
End Sub
End Class

Public Class SingletonException

Inherits System.ApplicationException

Public Sub New()
MyBase.New("Program already running!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Program already running!", InnerException)
End Sub

End Class

HTH,
Greg

"Noozer" <do*******@me.here> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I

know
programmatically if the application is already opened when user tries to

open
the second instance? Is there a way to prohibit user to launch more than

one
application at one time?


I am also interested in knowing if my application is already running when
it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already
running
instance of my app instead of spawning a second copy of my app?

Nov 21 '05 #6
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance
to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult.
I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:

' code to start app...
Try
SingletonApp.Run(New MyMainForm, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4")
'arbitrary GUID
Catch ex As SingletonException
MsgBox("Can not start because a previous instance of this application is
already running!")
End Try

' code inside singletonapp.vb file
Option Strict On

Imports System.Threading

Public Class SingletonApp

Private Shared _guid As String

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _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() ' think this may not be necessary???
m_Mutex.Close()
End Sub
End Class

Public Class SingletonException

Inherits System.ApplicationException

Public Sub New()
MyBase.New("Program already running!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Program already running!", InnerException)
End Sub

End Class

HTH,
Greg

"Noozer" <do*******@me.here> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I

know
programmatically if the application is already opened when user tries to

open
the second instance? Is there a way to prohibit user to launch more than

one
application at one time?


I am also interested in knowing if my application is already running when
it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already
running
instance of my app instead of spawning a second copy of my app?

Nov 21 '05 #7
"Nina" <Ni**@discussions.microsoft.com> schrieb:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application?
No, it won't.
How can I know programmatically if the application is already opened when
user tries to open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?


How do I make sure that only one instance of my application runs at a time?
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8
"Nina" <Ni**@discussions.microsoft.com> schrieb:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application?
No, it won't.
How can I know programmatically if the application is already opened when
user tries to open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?


How do I make sure that only one instance of my application runs at a time?
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult. I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:


<snip>

Thanks!
Nov 21 '05 #10

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult. I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:


<snip>

Thanks!
Nov 21 '05 #11
Nak
Hey Noozer,

Use remoting to make a single instance application. It's pretty easy to
do,

http://www.codeproject.com/vb/net/sing_inistan.asp

Nick.
Nov 21 '05 #12
Nak
Hey Noozer,

Use remoting to make a single instance application. It's pretty easy to
do,

http://www.codeproject.com/vb/net/sing_inistan.asp

Nick.
Nov 21 '05 #13
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #14
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #15
Nak
> Hey Noozer,

My "appologees", I meant Nina
Nov 21 '05 #16
Nak
> Hey Noozer,

My "appologees", I meant Nina
Nov 21 '05 #17
Thank you.

"Cor Ligthert" wrote:
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina


Nov 21 '05 #18
Thank you.

"Cor Ligthert" wrote:
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina


Nov 21 '05 #19
Thank you.

"Nak" wrote:
Hey Noozer,


My "appologees", I meant Nina

Nov 21 '05 #20
Thank you.

"Nak" wrote:
Hey Noozer,


My "appologees", I meant Nina

Nov 21 '05 #21
Thank you all for your valuable inputs.

"Nina" wrote:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina

Nov 21 '05 #22
Thank you all for your valuable inputs.

"Nina" wrote:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina

Nov 21 '05 #23

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

Similar topics

4
7103
by: @(none) | last post by:
Hi, I have a picturegallery that opens in a new window as a defined size, but I would like to open the popup in the middle of the screen. Some browsers do it like that, others don't... Any ideas...
6
907
by: Angelos Karantzalis | last post by:
Hi y'all ... I'm a bit puzzled here about .NET class instancing under COM+ Issue 1: I've a COM+ component, let's call it ... COMDbWrapper that initializes itself from an xml file. The data...
5
2198
by: Kamyk | last post by:
Hello all again! On the main form I have a few fields and among of them there is button which opens additional window (named "Links") where user can delete and add data from the database. I...
4
2035
by: Roland Riess | last post by:
Hi all, at the moment i am developing an app which is sort of an interface to copy data from one database to another and it shall run as a service. As there are several databases the app must be...
0
774
by: Nina | last post by:
While user is working on a windows application, at same time he or she launches another instance of the same application. Will this affect the global variables in the first instance of this...
6
2631
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. ...
8
10982
by: jaibux | last post by:
Everybody knows how to open (or clone) the same page that you are viewing in a new browser window by CTRL+N or via File->New Window. the question is how to PREVENT to open the SAME WEB APPLICATION...
3
4664
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
4
2674
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
7198
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
7072
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
7319
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
6979
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...
1
4998
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
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
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.