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 2427
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: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |