473,325 Members | 2,805 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,325 software developers and data experts.

2 instances of the program running

I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a second
instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)

Any help would be greatly appreciated!
Chris
ct******@pierceassociates.com
Nov 22 '05 #1
11 2423
Chris Thunell <ct******@pierceassociates.com> wrote:
I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a second
instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)


See http://www.pobox.com/~skeet/csharp/f...ation.instance

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Hi, Chris

this might be useful
http://msdn.microsoft.com/library/de...eaworapps1.asp
Check single instance section

HTH
Alex

"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a second instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)

Any help would be greatly appreciated!
Chris
ct******@pierceassociates.com

Nov 22 '05 #3
If you do a search for PrevInstance in VS/VB you will get the following code
block

Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrentProcess.ProcessName))
0 Then Return True
Else
Return False
End If
End Function

Just call it from Sub Main()

Sub Main
If Not PrevInstance Then
Dim MainForm as new Form1 '(or whatever)
Application.Run(MainForm)
End if
End Sub
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a
second
instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)

Any help would be greatly appreciated!
Chris
ct******@pierceassociates.com

Nov 22 '05 #4
Hi Chris,

This is in my opinion the most simple solution in VB

Private mut As New Threading.Mutex(True, "myProg", mutCreated)

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()

However this goes wrong when there is another program with the same name.

When that can be a problem than you can look to the page from Jon or to a
page from Herfried, the page from Herfried is in German. However you can
take the code.

<URL:http://dotnet.mvps.org/dotnet/code/application/>
-> "Nur eine Instanz der Anwendung zulassen"

I hope this helps,

Cor
Nov 22 '05 #5
Jared <VB***********@email.com> wrote:
If you do a search for PrevInstance in VS/VB you will get the following code
block

Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.Get
CurrentProcess.ProcessName))
> 0 Then

Return True
Else
Return False
End If
End Function


That's only useful if you know there won't be any other processes with
the same name, however. You can get a much better guarantee of
uniqueness using mutexes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6
This is the 3rd time in as many days I've posted this bit of code that I
use. I find searching Google groups very effective way to find information.
<wink><wink>

Since, nobody has commented on it, I am beginning to feel self concious. :)
I am curious if you guys wouldn't recommend this approach yourself? This was
code posted by another user quite sometime ago and seems to work as I
expect.

Greg
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...
'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


Nov 22 '05 #7
Hi Greg,

I thought that this was the code as Jon and Herfried have on there pages,
while Tom Shelton has showed it basicly as well what is presented on the
page of Herfried.

Therefore you get probably no comment.

If it is totally others than reply

I am always curious?

Cor
Nov 22 '05 #8
Your are correct Cor.

The mutex code here is basically the same
<URL:http://dotnet.mvps.org/dotnet/code/application/>

The German just shied my away was all.
Nov 22 '05 #9
This is the 3rd time in as many days I've posted this bit of code that I
use. I find searching Google groups very effective way to find information.
<wink><wink>

Since, nobody has commented on it, I am beginning to feel self concious. :)
I am curious if you guys wouldn't recommend this approach yourself? This was
code posted by another user quite sometime ago and seems to work as I
expect.

Greg
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...
'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


Nov 22 '05 #10
Hi Greg,

I thought that this was the code as Jon and Herfried have on there pages,
while Tom Shelton has showed it basicly as well what is presented on the
page of Herfried.

Therefore you get probably no comment.

If it is totally others than reply

I am always curious?

Cor
Nov 22 '05 #11
Your are correct Cor.

The mutex code here is basically the same
<URL:http://dotnet.mvps.org/dotnet/code/application/>

The German just shied my away was all.
Nov 22 '05 #12

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

Similar topics

8
by: Rahul | last post by:
Hi. I am part of a group in my univ where we organize a programming contest. In this contest we have a UDP based server. The server simulates a game and each contestant is to develop a team of...
3
by: Tim | last post by:
Hello Everyone I'd like to create a script/program that can locate all servers running DB2, finding out which instances are running/created and what databases are running under them but have...
4
by: PalB | last post by:
How to stop running multiple instances of my App in C#? Thanx
4
by: logicalfeline | last post by:
How do you prevent multiple instances of a program from running? Cat
4
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library...
4
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...
11
by: Clark Stevens | last post by:
I just finished a WinForms app in VB.NET. I want to allow the user to be able to run multiple instances of the program like you can with Notepad and Wordpad. The way it is now, once I run the...
22
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...
1
by: sunil | last post by:
Hello All. I have written a program as an exe that performs some kind of order processing. The program is first configured and then started manually. I have have multiple instances of this...
6
by: =?Utf-8?B?QnJvbmlzbGF2?= | last post by:
I need to run more then 1 instance of the same program but also I need to know which instance is running. In C++ code was: int nInstance = 0; char szAppName; m_dwMutexReturn =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.