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

Previnstance function

In the VB.Net documentation it says that there is no direct equivalent to
VB6's 'PrevInstance' function, so they suggest making your own with the
following code:

Function PrevInstance() As Boolean

If
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))
0) Then


Return True

Else

Return False

End If

End Function

The only problem is that the VB.NET documentation qualifies this with the
comment that "once a second instance is loaded, the first instance will also
return true". For example, as soon as a second app is opened while the first
is already open, I can force the new app instance to close. But ever after
that I get a value of TRUE back, even if one of our users is trying to open
the application after having gone through a situation where they opened it
twice. I end up having to get the user to log out and back in again to reset
the process count.

Is there a better way to detect and kill duplicate app instances? Where is
the process count stored ... in the registry? (Can I reset it from in the
program when I close a duplicate instance)?

Thanks for help in advance,

Tom
Nov 21 '05 #1
3 3961
Here's one way, but it doesn't seem to work with VB.NET 2002 in case you
have it

Public Function PrevInstance() As Boolean
If
UBound(System.Diagnostics.Process.GetProcessesByNa me(System.Diagnostics.Proc
ess.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function

Crouchie1998
BA (HONS) MCP MCSE
Official Microsoft Beta Tester
Nov 21 '05 #2
Hi Tom,

There is an alternate method which involves having your application
attempt to hold a uniquely named Mutex on startup.

If you cannot hold the mutex, it means that another instance of your
application has already taken hold of it (and thus another copy is running).

http://www.codeproject.com/managedcp...singleinst.asp

Have a look at that article. It's written in Managed C++, but you can
take the concept and apply it to VB.NET

If you don't know C++, here's an example:

---
Imports System.Threading

'...
Dim mut as Mutex
mut = New Mutex(false, "InsertYourUniqueStringHere")

If Not mut.WaitOne(1, true) Then
' application is already running
' (WaitOne returns FALSE if we can't get the lock)
MessageBox.Show("Another instance of this application is already
running.")

Application.Exit() ' if you're in Windows Forms
End If
'...
---

Just *make sure* you free the mutex before you end your application!

BTW: It might be a good idea to generate and use a GUID as your unique
string.

Regards,
-Adam.

Tom Edelbrok wrote:
In the VB.Net documentation it says that there is no direct equivalent to
VB6's 'PrevInstance' function, so they suggest making your own with the
following code:

Function PrevInstance() As Boolean

If
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))
> 0) Then


Return True

Else

Return False

End If

End Function

The only problem is that the VB.NET documentation qualifies this with the
comment that "once a second instance is loaded, the first instance will also
return true". For example, as soon as a second app is opened while the first
is already open, I can force the new app instance to close. But ever after
that I get a value of TRUE back, even if one of our users is trying to open
the application after having gone through a situation where they opened it
twice. I end up having to get the user to log out and back in again to reset
the process count.

Is there a better way to detect and kill duplicate app instances? Where is
the process count stored ... in the registry? (Can I reset it from in the
program when I close a duplicate instance)?

Thanks for help in advance,

Tom

Nov 21 '05 #3
Here's another way too:

http://www.vb2themax.com/ShowContent...b-c696b4c3d8a1

Crouchie1998
BA (HONS) MCP MCSE
Official Microsoft Beta Tester
Nov 21 '05 #4

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
3
by: David de Passos | last post by:
Hi! There is my code: Shared Function PrevInstance() As Boolean If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent Process.ProcessName)) > 0 Then Return True...
7
by: Jaime Lucci | last post by:
There is any instruction that replace the App.PrevInstance which is used in VB6? Thanks.
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
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
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...
0
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.