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 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
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
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
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
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
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
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
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.
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: PalB |
last post by:
How to stop running multiple instances of my App in C#?
Thanx
|
by: logicalfeline |
last post by:
How do you prevent multiple instances of a program from running?
Cat
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |